Center Screen Two Android Buttons


Recently I was into a problem when I was unable to center screen two Android buttons, in a simplest way. My Google skills failed me to find a simple way, so I had to figure it out myself. I wanted my buttons to be centered horizontally and vertically on the screen. And those buttons would have certain widths. Like this one:

Center Screen Two Android Buttons
Centered Two Android Buttons

I got it by using these codes:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/widget29"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>

    <LinearLayout 
    android:orientation="horizontal"
    android:layout_width="400dp"
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true">
    
        <Button 
        android:id="@+id/messagesBtn" 
        android:layout_width="0.0dip"
        android:layout_height="fill_parent" 
        android:text="Lovely Button"
        android:layout_weight="1.0" />
        
        <Button 
        android:id="@+id/performanceSummaryBtn" 
        android:layout_width="0.0dip"
        android:layout_height="fill_parent" 
        android:text="Sweet Button"
        android:layout_weight="1.0" />
        
    </LinearLayout>
    
</RelativeLayout>

That’s it! :)

,

Leave a Reply

Your email address will not be published. Required fields are marked *