54

I want to assign layout weights to several items within a LinearLayout inside of a ScrollView. However, the ScrollView ignores the LinearLayout weightSum.

My goal is to divide the layout with weights of 2, 1, 1 (for a total sum of 4), but this does not work properly inside of a ScrollView.

How can I solve this layout problem?

main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:weightSum="4">

        <LinearLayout android:id="@+id/logo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_weight="2"
            android:background="#FFFFFF" />

        <LinearLayout android:id="@+id/logo1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_weight="1"
            android:background="#000000" />


        <LinearLayout android:id="@+id/logobutton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" 
            android:layout_weight="1" 
            android:background="#4B4B4B" />

    </LinearLayout>
</ScrollView>
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
MIP TECH
  • 559
  • 2
  • 5
  • 9
  • if i am removing only scrollview than is work but my question is if am use scrollview than is not work – MIP TECH Apr 25 '12 at 08:57
  • check this one : [Android Layout Weight](http://stackoverflow.com/questions/4986861/android-layout-weight) I think it kind of the same problem. – user1264255 Apr 25 '12 at 08:57

5 Answers5

169

I have faced this problem before. Just use android:fillViewport="true" in your ScrollView and it will fill up the screen.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >
Melquiades
  • 8,496
  • 1
  • 31
  • 46
Akhil
  • 13,888
  • 7
  • 35
  • 39
3

This won't work as you have done it. The child view of a ScrollView should be set to wrap_content. If you set it to fill_parent, it will fill the area of the ScrollView and never scroll, because it won't be larger than the ScrollView.

The idea of layout_weight is to fill a specific area proportionately.

You should set all of the child LinearLayouts layout_height to either wrap_content or a specific size (in dp) and the parent LinearLayout layout_height to wrap_content

As said you need to remove the additional

xmlns:android="http://schemas.android.com/apk/res/android"

if it's not the root (first) element of the layout.

Tim Kist
  • 1,164
  • 1
  • 14
  • 38
David Scott
  • 1,666
  • 12
  • 22
1

just put this in your scroll view:

android:fillViewport="true"

Ali Keb
  • 43
  • 2
0

In my experience, fillviewport=true works bit strange.

I solved this problem by wrapping LinearLayout with another Layout like FrameLayout.

I hope this helps.

Elletlar
  • 3,136
  • 7
  • 32
  • 38
  • 3
    Hi Andy. Welcome to StackOverflow. Thank you for your answer. But this answer is very thin on details. You should show a sample that demonstrates the wrapping that you mention. See these [Tips for answering](https://stackoverflow.com/help/how-to-answer). Thank you. – Elletlar Mar 10 '19 at 09:00
0

Add below line in your ScrollView it will be work fine.

Only single child must be there for ScrollView

android:fillViewport="true"
Manju S B
  • 101
  • 1
  • 4