Android align button to center and make sure they are evenly spaced out

79 views Asked by At

enter image description here

I got a Main Menu with four buttons (attached), I would like to align all buttons to the center (including the "Main Menu" textView), and I would like to make sure that each buttons are all evenly spaced out.

I added android:gravity="center"> but its not aligning to center. Also to evenly space out the buttons I tried to add android:layout_weight="1" as one of the Button attributes but my button disappears from the screen.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center">

   <TextView android:text="Main Menu" 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" />
    <Button android:text="Button" 
            android:id="@+id/button1"
            android:layout_width="wrap_content" 
            android:layout_height="0dp"/>
    <Button android:text="Button" 
            android:id="@+id/button2"
            android:layout_width="wrap_content" 
            android:layout_height="0dp"/>
    <Button android:text="Button" 
            android:id="@+id/button3"
            android:layout_width="wrap_content" 
            android:layout_height="0dp"/>
    <Button android:text="Button" 
            android:id="@+id/button4"
            android:layout_width="wrap_content" 
            android:layout_height="0dp"/>

</LinearLayout>
3

There are 3 answers

0
Nikos Mitropoulos On BEST ANSWER

Change your LinearLayout width and height from wrap_content to match_parent..like this..

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

   <TextView android:text="Main Menu" 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" />
    <Button android:text="Button" 
            android:id="@+id/button1"
            android:layout_width="wrap_content" 
            android:layout_height="55dp"/>
    <Button android:text="Button" 
            android:id="@+id/button2"
            android:layout_width="wrap_content" 
            android:layout_height="55dp"/>
    <Button android:text="Button" 
            android:id="@+id/button3"
            android:layout_width="wrap_content" 
            android:layout_height="55dp"/>
    <Button android:text="Button" 
            android:id="@+id/button4"
            android:layout_width="wrap_content" 
            android:layout_height="55dp"/>

</LinearLayout>
0
Syed Raza Mehdi On
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Main Menu" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>
0
balaji koduri On

no need to hard code the height of the view or 0dp, if you want to set the waight then only height is 0dp. check all the line i given here.

try this:

<?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="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main Menu" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>