2

I am trying to make a button anchored to the bottom of the view and also bottom of the list if the list gets bigger than the view.

So I want them to look like this when list is smaller than screen:

enter image description here

and like this when bigger:

enter image description hereenter image description here

I know I should use RelativeLayout to place the button at the bottom of the page with android:layout_alignParentBottom="true" but when I also add android:layout_below="@id/table_wrapper" my button's height will get messed up if content is smaller than view:

enter image description here

Is there anyone who know how I can accomplish this?

The simplified version of my code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/book_layout_whole"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:fillViewport="true">

        <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            <LinearLayout
                    android:id="@+id/table_wrapper"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                <LinearLayout
                        android:id="@+id/vertical_header_table"
                        android:layout_width="85dp"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:text="Day"
                            android:gravity="center"/>
                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:text="Day"
                            android:gravity="center"/>
                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:text="Day"
                            android:gravity="center"/>
                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:text="Day"
                            android:gravity="center"/>
                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:text="Day"
                            android:gravity="center"/>
                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:text="Day"
                            android:gravity="center"/>
                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:text="Day"
                            android:gravity="center"/>
                </LinearLayout>

                <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
                    <View
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:background="#444444"></View>
                    <View
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:background="#444444"></View>
                    <View
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:background="#444444"></View>
                    <View
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:background="#444444"></View>
                    <View
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:background="#444444"></View>
                    <View
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:background="#444444"></View>
                    <View
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:background="#444444"></View>
                </LinearLayout>
            </LinearLayout>
            <Button
                    android:text="load more"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/table_wrapper"
                    android:layout_alignParentBottom="true"
                    android:gravity="center"
                    android:padding="20dp"
                    />
        </RelativeLayout>
    </ScrollView>
</LinearLayout>

Note 1:

removing android:layout_below="@id/table_wrapper" will result in the button always being at the bottom of the screen, even when there list is bigger than the screen.

Note 2

Adding layout_weight would not work, because the size of my view is dynamic.

maclir
  • 3,218
  • 26
  • 39

2 Answers2

1

Then use following, it will surely help you, i have tested it !

      <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/table_wrapper" >

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="20dp"
            android:text="load more" />
    </LinearLayout>
maclir
  • 3,218
  • 26
  • 39
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
0

I don't know why my post was deleted By George Stocker

Check it maclir should work for you:

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

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true" >

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

            <LinearLayout
                android:id="@+id/table_wrapper"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:orientation="horizontal" >

                <LinearLayout
                    android:id="@+id/vertical_header_table"
                    android:layout_width="85dp"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="Day" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="Day" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="Day" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="Day" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="Day" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="Day" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:gravity="center"
                        android:text="Day" />
                </LinearLayout>

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

                    <View
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:background="#444444" >
                    </View>

                    <View
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:background="#444444" >
                    </View>

                    <View
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:background="#444444" >
                    </View>

                    <View
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:background="#444444" >
                    </View>

                    <View
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:background="#444444" >
                    </View>

                    <View
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:background="#444444" >
                    </View>

                    <View
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:background="#444444" >
                    </View>
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="5.5"
                android:orientation="vertical" >

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:padding="20dp"
                    android:text="load more" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>
Guilherme Gregores
  • 1,050
  • 2
  • 10
  • 27