codeofaninja
website

Center Screen Two Android Buttons

Photo of Mike Dalisay
Modified Sunday, August 14, 2011
by - @ninjazhai
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 Buttons in Android
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!  :)
For FREE programming tutorials, click the red button below and subscribe! :)
Thanks for the comments!
 
 
Fundamentals
"First do it, then do it right, then do it better."
~ Addy Osmani
"Talk is cheap. Show me the code."
~ Linus Torvalds
Let's Stay Connected!
g+ r
Android app on Google Play
© 2011-2014 The Code Of A Ninja. All rights reserved. Proudly Powered by Google Blogger. Images, logos, marks or names mentioned herein are the property of their respective owners.