Anand
Anand

Reputation: 1973

How to auto align the buttons to fit in center of the screen

Newbie to android, Stuck with aligning the button to center of the screen.

here the code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.kissmat.gbrf_ebook.SingupIntro"
    android:background="@drawable/lib_gradient">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Author"
        android:textColor="#ffffffff"
        android:background="#00ffffff"
        android:layout_marginLeft="70dp"
        android:layout_marginStart="70dp"
        android:layout_marginTop="82dp"
        android:id="@+id/authorBtn"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:elegantTextHeight="true"
        android:drawableTop="@drawable/author" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:drawableTop="@drawable/publisher"
        android:text="PUBLISHER"
        android:textColor="#ffffffff"
        android:background="#00ffffff"
        android:id="@+id/pubBtn"
        android:layout_alignTop="@+id/authorBtn"
        android:layout_toRightOf="@+id/authorBtn"
        android:layout_toEndOf="@+id/authorBtn"
        android:layout_marginLeft="48dp"
        android:layout_marginStart="58dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:drawableTop="@drawable/student"
        android:text="STUDENT"
        android:textColor="#ffffffff"
        android:background="#00ffffff"
        android:layout_marginTop="57dp"
        android:id="@+id/stbBtn"
        android:layout_below="@+id/authorBtn"
        android:layout_alignLeft="@+id/authorBtn"
        android:layout_alignStart="@+id/authorBtn" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:drawableTop="@drawable/user"
        android:text="READER"
        android:textColor="#ffffffff"
        android:background="#00ffffff"
        android:id="@+id/readerBtn"
        android:layout_alignTop="@+id/stbBtn"
        android:layout_alignLeft="@+id/pubBtn"
        android:layout_alignStart="@+id/pubBtn" />
</RelativeLayout>

How it looks when i run on the device.

enter image description here

Upvotes: 0

Views: 224

Answers (1)

Thanos
Thanos

Reputation: 3665

I just removed the margins (Left, Start and Top) from your first button and added the property android:gravity="center" to your layout. The last one positions the contents in the center. There's no need to do that with fixed margin values. This is very helpful as your layout will be centered in all the different screen sizes and pixel densities.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.kissmat.gbrf_ebook.SingupIntro"
    android:background="@drawable/lib_gradient"
    android:gravity="center">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Author"
        android:textColor="#ffffffff"
        android:background="#00ffffff"
        android:id="@+id/authorBtn"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:elegantTextHeight="true"
        android:drawableTop="@drawable/author" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:drawableTop="@drawable/publisher"
        android:text="PUBLISHER"
        android:textColor="#ffffffff"
        android:background="#00ffffff"
        android:id="@+id/pubBtn"
        android:layout_alignTop="@+id/authorBtn"
        android:layout_toRightOf="@+id/authorBtn"
        android:layout_toEndOf="@+id/authorBtn"
        android:layout_marginLeft="48dp"
        android:layout_marginStart="58dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:drawableTop="@drawable/student"
        android:text="STUDENT"
        android:textColor="#ffffffff"
        android:background="#00ffffff"
        android:layout_marginTop="57dp"
        android:id="@+id/stbBtn"
        android:layout_below="@+id/authorBtn"
        android:layout_alignLeft="@+id/authorBtn"
        android:layout_alignStart="@+id/authorBtn" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:drawableTop="@drawable/user"
        android:text="READER"
        android:textColor="#ffffffff"
        android:background="#00ffffff"
        android:id="@+id/readerBtn"
        android:layout_alignTop="@+id/stbBtn"
        android:layout_alignLeft="@+id/pubBtn"
        android:layout_alignStart="@+id/pubBtn" />
</RelativeLayout>

Upvotes: 1

Related Questions