0
<Button
    android:id="@+id/button1"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:text="Click Me" />

<Button
    android:id="@+id/button2"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/button1"
    android:text="Click me1" />

<Button
    android:id="@+id/button3"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:text="Click Me2" />

<Button
    android:id="@+id/button4"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/button3"
    android:layout_below="@+id/button2"
    android:text="Click me3" />

I tried to use this code for main activity using relative layout but I want to align them all in sync in the centre of the screen. Can anybody help me here plz?

I want to achieve an output such that it should look like:

       Button1    Button2
       Button3    Button4

And the whole set should be collectively in centre of the screen whatsoever is the screen size.

Amrit
  • 2,295
  • 4
  • 25
  • 42

4 Answers4

3

Use LinearLayout with layout_width = "maych_parent", layout_height="match_parent", gravity="center", orientation="vertical"

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

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

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

    <Button
        android:id="@+id/button2"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Click me1" />
</LinearLayout>

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

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

    <Button
        android:id="@+id/button4"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Click me3" />
</LinearLayout>


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

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

    <Button
        android:id="@+id/button4"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Click me3" />
</LinearLayout>

</LinearLayout>
Pawan Yadav
  • 1,772
  • 19
  • 17
  • Thanks for your time. The example you gave will align the button in vertical order in the centre of the screen. Thats ok. But how do I do like button1 button2 then below these two it will be button3 button4 and all collectively in center. – Amrit May 09 '13 at 09:38
0

Try this:

layout_width="fill_parent"
layout_marginLeft="10dip"
layout_marginRight="10dip"

You can also use a Relative layout and set the child to android:layout_centerInParent="true"

Linga
  • 10,379
  • 10
  • 52
  • 104
0

Adjust layout_y and _x then it will be place in center

<Button
android:id="@+id/Button01"
android:layout_width="188px"
android:layout_height="100px"
android:text="A"
android:layout_y="50px" android:layout_x="65px" android:textSize="48sp"/>
0

Using Linearlayout with gravity center

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

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

<Button
    android:id="@+id/button2"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/button1"
    android:text="Click me1" />

<Button
    android:id="@+id/button3"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:text="Click Me2" />

<Button
    android:id="@+id/button4"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/button3"
    android:layout_below="@+id/button2"
    android:text="Click me3" />


</LinearLayout>

Output

Muhannad A.Alhariri
  • 3,702
  • 4
  • 30
  • 46
  • Thanks for your time. The example you gave will align the button in vertical order in the centre of the screen. Thats ok. But how do I do like button1 button2 then below these two it will be button3 button4 and all collectively in center. – Amrit May 09 '13 at 09:40
  • @LetsCode You can use Linear layout for each 2 buttons , with orientation horizontal for each linear layout – Muhannad A.Alhariri May 09 '13 at 09:41