1

I'm trying to put the textview at the bottom of the layout, but it doesn't work. Please help me to get the textview to the bottom of the screen. I tried gridlayout but I don't really understand the way to use it.

Thank you

<?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"
        android:background="@drawable/background1" >

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

        <TextView
                android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:gravity="center"
                android:text="Tour Main Menu"
                android:textSize="@dimen/font_large" />   
    </LinearLayout> 

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

        <TextView
                android:id="@+id/tour_info"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="30dp"
                android:gravity="center"
                android:text="There are 47 Passengers"
                android:textSize="@dimen/font_medium" />   

    </LinearLayout> 

    <TableLayout  
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:shrinkColumns="*"
            android:paddingRight="5dp"
            android:orientation="vertical" >
        <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >


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

                <ImageButton
                        android:id="@+id/passenger_list"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:gravity="center_horizontal|center_vertical"
                        android:background="@drawable/icon"
                        android:contentDescription="@string/pass_list"  />    

                <TextView
                        android:id="@+id/passenger_list_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/pass_list"
                        android:textSize="@dimen/font_medium" />

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

                <ImageButton
                        android:id="@+id/arrivals"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal|center_vertical"
                        android:background="@drawable/icon"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:padding="44dp"
                        android:contentDescription="@string/arrivals" />

                <TextView
                        android:id="@+id/arrivals_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/arrivals"
                        android:textSize="@dimen/font_medium" />

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

                <ImageButton
                        android:id="@+id/departures"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/icon"
                        android:contentDescription="@string/departures" />

                <TextView
                        android:id="@+id/departures_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/departures"
                        android:textSize="@dimen/font_medium" />    .

            </LinearLayout>

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:id="@+id/master_rooming"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/icon"
                        android:contentDescription="@string/rooming" />

                <TextView
                        android:id="@+id/master_rooming_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/rooming"
                        android:textSize="@dimen/font_medium" />        
            </LinearLayout>
        </TableRow>

        <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="50dp" >


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

                <ImageButton
                        android:id="@+id/itinerary"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/icon"
                        android:contentDescription="@string/itinerary" />   

                <TextView
                        android:id="@+id/itinerary_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/itinerary"
                        android:textSize="@dimen/font_medium" />    

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

                <ImageButton
                        android:id="@+id/qa"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/icon"
                        android:contentDescription="QA" />  

                <TextView
                        android:id="@+id/qa_text"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="QA"
                        android:textSize="@dimen/font_medium" />

            </LinearLayout>
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/icon"
                        android:contentDescription="QA" />  

                <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="Text Here"
                        android:textSize="@dimen/font_medium" />

            </LinearLayout>

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:layout_marginLeft="10dp"
                        android:background="@drawable/icon"
                        android:contentDescription="QA" />  

                <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="Text Here"
                        android:textSize="@dimen/font_medium" />

            </LinearLayout>
        </TableRow> 
    </TableLayout>


    <!-- Time Stamp at the bottom of the screen -->

    <TextView
        android:id="@+id/timeStamp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom|center"
        android:text=""
        android:textSize="@dimen/font_medium" />

</LinearLayout>
4
  • 2
    give layout_gravity a shot?.. also, you've got this textview in a linearlayout; I'de recommend containing everything within a relative layout (and then alignParentBottom=true + gravity) OR use a framelayout, and use layout_gravity. Feb 27, 2013 at 19:06
  • Yes, follow Nirvana Tikku's suggestion to wrap the whole thing in a RelativeLayout rather than a LinearLayout and use alignParentBottom=true on the TextView.
    – dhaag23
    Feb 27, 2013 at 19:12
  • I also did relative layout before, but if I use the relative layout on a different dpi screen, it will look different correct? I'm trying to keep the same look on different devices.
    – Mike
    Feb 27, 2013 at 20:33
  • "the same look" <-- wouldn't you expect the textview to be at the bottom of the screen, then? a relative layout will give you the flexibility to define the positioning of items, and will scale proportionally.. Feb 27, 2013 at 21:06

1 Answer 1

4

Try this:

<?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"
    android:background="@drawable/background1" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="0"
        >
    ...   
    </LinearLayout> 

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="0"
        >
    ...   
    </LinearLayout> 

    <TableLayout  
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingLeft="5dp"
        android:shrinkColumns="*"
        android:paddingRight="5dp"
        android:orientation="vertical" >
    ...   
    </TableLayout>


    <!-- Spacer: stretches causing the TextView below to move to the bottom of the screen -->
    <View 
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
    />

    <!-- Time Stamp at the bottom of the screen -->

    <TextView
        android:id="@+id/timeStamp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text=""
        android:textSize="@dimen/font_medium" />

</LinearLayout>

The idea is to attach android:layout_weight to each child of LinearLayout. All of them have a weight of 0, meaning, they do not stretch to fill available space. Except one, the View. It has a weight of 1, meaning it will receive all available space and stretch, effectively moving the TextView below to the bottom.

1
  • Note that you could get rid of the spacer View and attach android:layout_weight="1" to one of the already existing children in your layout, allowing them to stretch so that the last child (TextView) will be put on the bottom. Feb 27, 2013 at 19:15

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.