-1

I am trying to arrange 3 buttons vertically in the centre of the available space in an Android Activity. I also have a title field in a TextView and I need this to be at the top of the display screen with a vertical gap between it and the buttons. I also need the blank space between the bottom button and the bottom of the screen to be the same as the space between the top button and the bottom of the TextView.

From reading the docs istm that a pair of nested vertical LinearLayouts should do the job as below but this leaves me with the buttons arranged immediately following the TextView with no space between the TextView and the top button.

Any ideas how I can achieve my aim would be gratefully received.

<?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" >

    <TextView android:id="@+id/app_title_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_title_label"
        android:gravity="top"
        style="?android:listSeparatorTextViewStyle"
    />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:layout_gravity="center" >

        <Button android:id="@+id/button_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
        />

        <Button android:id="@+id/button_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
        />

        <Button android:id="@+id/button_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
        />

    </LinearLayout>

</LinearLayout>

Thanks,

Boo
  • 165
  • 2
  • 11

4 Answers4

0

Give weight to the second layout

<TextView android:id="@+id/app_title_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/app_title_label"
    android:gravity="top"
    style="?android:listSeparatorTextViewStyle"
/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:layout_weight="1"
    android:layout_gravity="center" >

    <Button android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
    />

    <Button android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
    />

    <Button android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
    />

</LinearLayout>

This should fix your problem

Ajit Pratap Singh
  • 1,299
  • 12
  • 24
0

Use gravity instead of layout_gravity

<?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" >

    <TextView android:id="@+id/app_title_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_title_label"
        android:gravity="top"
        style="?android:listSeparatorTextViewStyle" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:gravity="center" >

        <Button android:id="@+id/button_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp" />

        <Button android:id="@+id/button_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp" />

        <Button android:id="@+id/button_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp" />

    </LinearLayout>

</LinearLayout>
Community
  • 1
  • 1
Kaushik
  • 6,150
  • 5
  • 39
  • 54
0

Try this code:- copy this xml file

  <?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:layout_marginTop="40dp"
     android:orientation="vertical" >

 <TextView
    android:id="@+id/textView1"
    android:layout_width="162dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="30dp"
    android:layout_marginTop="40dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 <Button
    android:id="@+id/button1"
    android:layout_width="236dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="40dp"
    android:text="Button" />

 <Button
    android:id="@+id/button2"
    android:layout_width="246dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:text="Button" />

 <Button
    android:id="@+id/button3"
    android:layout_width="239dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="40dp"
    android:layout_marginTop="40dp"
    android:text="Button" />

   </LinearLayout>

I have use this line for arranging the button after the textview

 android:layout_marginTop="40dp" 
user3173628
  • 182
  • 5
0
android:gravity="center" 

instead of

android:layout_gravity="center"

the working design is added below

  <?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" >

<TextView
    android:id="@+id/app_title_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:text="hifgd" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

</LinearLayout>
Arun Antoney
  • 4,292
  • 2
  • 20
  • 26