1

I have a problem making a ScrollView wrap the content.

I don't know why but the ScrollView's height is bigger then the content I have and I read about some way to fix this but they didn't work.

(layout_weight, fill_parents, wrap_content, android:fillViewport="true").

So I guess I have made a simple mistake in my code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/title_bg"
        android:gravity="center"
        android:text="Profile"
        android:textColor="#fff"
        android:textSize="20sp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true" >

        <LinearLayout
            android:id="@+id/LinearLayout01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/background"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/TextView02"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:text="@string/mother"
                    android:textAppearance="? android:attr/textAppearanceMedium" />

                <EditText
                    android:id="@+id/etMother"
                    android:layout_width="0sp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_weight="1"
                    android:ems="10"
                    android:hint="@string/enter_your_name"
                    android:inputType="textPersonName" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <ToggleButton
                    android:id="@+id/tbDueDateLMP"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:background="@drawable/button_red"
                    android:checked="true"
                    android:textColor="#fff"
                    android:textOff="@string/due_date"
                    android:textOn="@string/lmp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/bPlusDay"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="1"
                    android:background="@drawable/button_blue"
                    android:onClick="onClick"
                    android:text="@string/plus"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/bPlusMonth"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="1"
                    android:background="@drawable/button_blue"
                    android:onClick="onClick"
                    android:text="@string/plus"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/bPlusYear"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.69"
                    android:background="@drawable/button_blue"
                    android:onClick="onClick"
                    android:text="@string/plus"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/tvDay"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="1"
                    android:background="@drawable/black_back"
                    android:padding="3dp"
                    android:text="00"
                    android:textColor="#000"
                    android:textSize="59sp" />

                <TextView
                    android:id="@+id/tvMonth"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="1"
                    android:background="@drawable/black_back"
                    android:padding="3dp"
                    android:text="00"
                    android:textColor="#000"
                    android:textSize="59sp" />

                <TextView
                    android:id="@+id/tvYear"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.69"
                    android:background="@drawable/black_back"
                    android:padding="3dp"
                    android:text="0000"
                    android:textColor="#000"
                    android:textSize="59sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/bMinusDay"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginTop="4dp"
                    android:layout_weight="1"
                    android:background="@drawable/button_blue"
                    android:onClick="onClick"
                    android:text="@string/minus"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/bMinusMonth"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginTop="4dp"
                    android:layout_weight="1"
                    android:background="@drawable/button_blue"
                    android:onClick="onClick"
                    android:text="@string/minus"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/bMinusYear"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="4dp"
                    android:layout_weight="0.69"
                    android:background="@drawable/button_blue"
                    android:onClick="onClick"
                    android:text="@string/minus"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <Button
                android:id="@+id/bProfileSave"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:background="@drawable/button_pink"
                android:onClick="onClick"
                android:text="@string/save"
                android:textColor="#fff" />

            <TextView
                android:id="@+id/tvProfileInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#F00" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

thank you all.

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Blackstar
  • 97
  • 1
  • 2
  • 8

4 Answers4

2

Please have a look at this code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"   
    android:background="@android:color/darker_gray"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="TEST" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/LinearLayout01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/TextView02"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:text="mother" />

                <EditText
                    android:id="@+id/etMother"
                    android:layout_width="0sp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_weight="1"
                    android:ems="10"
                    android:hint="enter_your_name"
                    android:inputType="textPersonName" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <ToggleButton
                    android:id="@+id/tbDueDateLMP"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                  
                    android:checked="true"
                    android:textColor="#fff"
                    android:textOff="due_date"
                    android:textOn="lmp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/bPlusDay"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="1"
                       
                    android:onClick="onClick"
                    android:text="plus"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/bPlusMonth"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="1"                   
                    android:onClick="onClick"
                    android:text="plus"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />
    
                <Button
                    android:id="@+id/bPlusYear"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.69"                   
                    android:onClick="onClick"
                    android:text="plus"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/tvDay"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="1"                   
                    android:padding="3dp"
                    android:text="00"
                    android:textColor="#000"
                    android:textSize="59sp" />

                <TextView
                    android:id="@+id/tvMonth"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="1"                   
                    android:padding="3dp"
                    android:text="00"
                    android:textColor="#000"
                    android:textSize="59sp" />

                <TextView
                    android:id="@+id/tvYear"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.69"                 
                    android:padding="3dp"
                    android:text="0000"
                    android:textColor="#000"
                    android:textSize="59sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/bMinusDay"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginTop="4dp"
                    android:layout_weight="1"                  
                    android:onClick="onClick"
                    android:text="minus"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/bMinusMonth"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginTop="4dp"
                    android:layout_weight="1"                
                    android:onClick="onClick"
                    android:text="minus"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/bMinusYear"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="4dp"
                    android:layout_weight="0.69"                   
                    android:onClick="onClick"
                    android:text="minus"
                    android:textColor="#fff"
                    android:textSize="20sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <Button
                android:id="@+id/bProfileSave"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"               
                android:onClick="onClick"
                android:text="save"
                android:textColor="#fff" />

            <TextView
                android:id="@+id/tvProfileInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#F00" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31
0

You can try to set the top linear layout to wrap vertical content rather than fill parent, which make whole layout to use full height

Arcadien
  • 2,258
  • 16
  • 26
0

I think the culprit is in android:fillViewport="true". If this is set then it will fill the view port even if there is not enough content to take up the space and even though you have the conflicting attribute android:layout_height="wrap_content". fillViewport takes precedence.

alphonzo79
  • 938
  • 11
  • 12
0

Can try to set the android:layout_height = "0dp" It works like a charm

Ehsan Rosdi
  • 835
  • 9
  • 18