Reputation: 13
I can't make the Buttons
center on the screen. The text on top is in center, but the next 2 Buttons
are lying left and vertical center. How I can make them horizontal center as well? T
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dip"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
tools:ignore="HardcodedText" >
<TextView
android:id="@+id/lnTotal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="16sp"/>
<Button
android:id="@+id/btnAdd"
android:layout_width="250sp"
android:layout_height="wrap_content"
android:text="Add one"
android:textSize="12sp"
android:layout_below="@+id/lnTotal"/>
<Button
android:id="@+id/btnSubtract"
android:layout_width="250sp"
android:layout_height="wrap_content"
android:text="Subtract one"
android:textSize="12sp"
android:layout_below="@+id/btnAdd"/>
</RelativeLayout>
Upvotes: 0
Views: 132
Reputation: 65
This depends on which layout you're using. It is much easier to center a button using a RelativeLayout.
Here is an example of how a button can be centered using a RelativeLayout:
<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"
tools:context=".Mainpage">
<Button
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_round" />
</RelativeLayout>
As you can probably tell, the two lines of code that are centering the Button in the middle android:layout_centerVertical="true"
and android:layout_centerHorizontal="true"
Upvotes: 1