42

I want to implement this: A ScrollView that contains many elements (ImageViews, TextViews, EditTexts etc) and then after the ScrollView some buttons (which are custom ImageViews) that appear always exactly at the bottom of the screen.

If I use the android:fillViewport="true" attribute, then if the elements of the ScrollView are too big to fit in the screen size the buttons get invisible . If I use the android:Weight=1 attribute then the ScrollView gets only 50% of the Screen when the screen is big and it can fit (I want the buttons to take a small percentage, about 10%). If I set the android:Weight to bigger values then the buttons appear very small.

Please help! Maybe it is something simple that I overlooked but I’ve been banging my head for hours!

george
  • 1,386
  • 5
  • 22
  • 38

5 Answers5

95

Just created and tested it. Looks like you want.

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_alignParentBottom="true">
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Custom Button1"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Custom Button2"/>
    </LinearLayout>

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/buttons">
         <!--Scrollable content here-->
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">                
            <TextView
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="test text"
                    android:textSize="40dp"/>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>
Oleksii Masnyi
  • 2,922
  • 2
  • 22
  • 27
  • I had a similar problem. Worked well with this solution – zolio Oct 15 '12 at 01:34
  • 1
    When the ScrollView fills up, won't it write over the buttons? – FractalBob Sep 17 '15 at 03:58
  • @aleksey-masny: I have the similar problem, but I want to use **TextEdit** instead of **TextView**, something like a wysiwyg editor that has *Bold*, *Italic* and ... buttons on the bottom of screen and above keyboard. I did as you said but I could not fill the screen with TextEdit. It says something like `elements in ScrollView must have wrap_content as their height value`! could you help me! thanks – Alireza Oct 03 '15 at 11:14
  • 1
    hi am having layout with headder and scrolview of input fields and footer when keyboard is coming that footer also scroll up.how to fix that footer in bottom.please help me – Harsha Nov 18 '15 at 05:41
  • This is it! Perfect! Thanks man! This is a basic implementation of Relative layout, yet very important. – Kaveesh Kanwal Jan 12 '17 at 06:17
  • Thank you. Place button at **top in layout hierarchy** & then use of **android:layout_above="@id/buttons"** did the trick. – CoDe Dec 20 '17 at 01:03
  • fill_parent parent is deprecated! – Ali mohammadi Jan 14 '18 at 05:13
  • Can anyone please help to solve this issue with Constraintlayout.? Thank you. – VP4Android Nov 12 '21 at 23:00
  • `android:layout_above="@id/buttons"` what I was missing. Thank you! – beginner Apr 05 '22 at 05:49
12
<?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">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TextView 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Hello World"
            />
            <TextView 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Hallo Welt"
            />       
        </LinearLayout>
    </ScrollView>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go next page"
            android:layout_alignParentRight="true" />
    </RelativeLayout>

</LinearLayout>
macio.Jun
  • 9,647
  • 1
  • 45
  • 41
  • 1
    hi am having layout with headder and scrolview of input fields and footer when keyboard is coming that footer also scroll up.how to fix that footer in bottom.please help me – Harsha Nov 18 '15 at 05:34
  • It worked i was struggle to find solution for past 3hours . – chanakya Jan 11 '17 at 02:47
3

This worked for me. Give the scroll view a weight of 1. Put all the other widgets following the scroll view in a layout. The scroll view will grow enough to not block the rest.

Widgets in scroll view and rest at bottom

quiet_penguin
  • 808
  • 7
  • 18
2

scrollview cannot fit the screen because you put it on a linear layout, so linear layout fit in the screen,

just try to make scrollview as root elemen on xml layout

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <!-- Here you can put some XML stuff and BOOM! your screen fit to scrollview -->

    </LinearLayout>
</ScrollView>
kechvp
  • 39
  • 1
1

If you do not want to use RelativeLayout, it is better to use LinearLayout. This method is better in my opinion.

Just set the layout_weight to one

enter image description here

gadolf
  • 1,035
  • 11
  • 19