0

How can I make such layout? Header should not scrolls and should be always visible.

enter image description here

The matter is Android throws an error when I try to put ScrollView into LinearLayout.

Markup:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              style="@style/baseLayout">
    <LinearLayout
        style="@style/app_header" />
    <ScrollView
        style="@style/fillParent">
        <!-- elements here -->
    </ScrollView>
</LinearLayout>

Styles:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="app_theme" parent="android:Theme">
    </style>

    <style name="fillParent">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
    </style>
    <style name="fullWidth">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
    <style name="wrapContent">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="baseLayout" parent="fillParent">
        <item name="android:orientation">vertical</item>
        <item name="android:background">@drawable/app_bg</item>
    </style>

    <style name="app_header" parent="fullWidth">
        <item name="android:background">@drawable/header_bg</item>
    </style>
</resources>
1
  • Can you give us the exact error you are getting?
    – prolink007
    Mar 20, 2012 at 13:25

3 Answers 3

2

The following code works for me, could you post your code and let us see what you might be doing wrong?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TITLE BAR"/>
    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>
</LinearLayout>

Solution

The problem was that Ninja had more than 1 direct child in the ScrollView.

5
  • I am taking a look now. Thanks for the additional info.
    – prolink007
    Mar 20, 2012 at 13:21
  • What is the purpose of the LinearLayout style="@style/app_header" /> Right now it seems like it is empty and not needed. You could try putting a TextView there instead and see what happens. You can change the background of TextView.
    – prolink007
    Mar 20, 2012 at 13:28
  • That would be a problem, yes. ScrollView only accepts one direct child. Also, appserv just mentioned this issue while you were making this comment. And R.daneel.olivaw mentioned in a comment to his answer.
    – prolink007
    Mar 20, 2012 at 13:37
  • The fact is I've found a problem only after your question "Can you give us the exact error you are getting?" :) Mar 20, 2012 at 13:39
  • lol, i do that too. I have a few questions on SO where i have asked the question on here, then went back to look at the error a little deeper and then found the problem and posted my own answer. Taking a step back and typing up the problem usually helps me find my issues too.
    – prolink007
    Mar 20, 2012 at 13:40
1

It's not normal if there is an error if you put a Linear layout in side a Scroll view. Please try the below snippet in your code.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TextView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TITLE BAR"/>

  <ScrollView 
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:orientation="vertical" >

      <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        .
        .
      </LinearLayout>

  </ScrollView>

</LinearLayout>

I hope this helps.

3
  • DotNET Ninja said they were trying to put a ScrollView inside a LinearLayout not the other way around. Also, did you just copy what i did? lol
    – prolink007
    Mar 20, 2012 at 13:16
  • What he means is that in your case we will have to do that and also have a linear layout inside the scroll view, as in android scrollview can have only 1 child. So all the scrollable content will be inside the inner linear layout. Mar 20, 2012 at 13:18
  • Yes, but just the <TextView> tag, I am sorry if it offended you. Mar 20, 2012 at 13:29
1

In your markup:

 ....  
 <ScrollView
      style="@style/fillParent">
       <!-- elements here -->
 </ScrollView>
 ... 

"elements here" - If more than one then you will get an error, because ScrollView can host only one direct child (for example layout).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.