Reputation: 45911
I'm developing an Android application.
I'm trying to set buttons save and close centered, but they appear to the left.
Here is my XML layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/dialogGameName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="2sp"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp"
android:typeface="sans"
android:textSize="26sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/dialogGameDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_marginBottom="5sp"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="2sp"
android:typeface="monospace"
android:textSize="18sp"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal">
<Button
android:id="@+id/registerGameButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:gravity="center"
android:layout_marginBottom="5sp"
android:layout_marginLeft="5sp"
android:layout_marginRight="2sp"
android:layout_marginTop="0sp" />
<Button
android:id="@+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/close"
android:gravity="center"
android:layout_marginBottom="5sp"
android:layout_marginLeft="2sp"
android:layout_marginRight="5sp"
android:layout_marginTop="0sp"/>
</LinearLayout>
</LinearLayout>
Upvotes: 0
Views: 19629
Reputation: 34823
Some tips for u (The change i made in your xml)
android:layout_gravity
is for the layout for its child we need to set android:gravity
to get exact center we set android:gravity="center"
In order to set the child in exact center the parent should be with android:layout_width="fill_parent"
and android:layout_height="fill_parent"
if that layout is a child of any other layout its should also be with android:layout_width="fill_parent"
and android:layout_height="fill_parent"
<TextView
android:id="@+id/dialogGameName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="2sp"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp"
android:text="asdff"
android:typeface="sans"
android:textSize="26sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/dialogGameDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="asdff"
android:layout_marginBottom="5sp"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="2sp"
android:typeface="monospace"
android:textSize="18sp"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/registerGameButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:gravity="center"
android:layout_marginBottom="5sp"
android:layout_marginLeft="5sp"
android:layout_marginRight="2sp"
android:layout_marginTop="0sp" />
<Button
android:id="@+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/close"
android:gravity="center"
android:layout_marginBottom="5sp"
android:layout_marginLeft="2sp"
android:layout_marginRight="5sp"
android:layout_marginTop="0sp"/>
</LinearLayout>
Upvotes: 1
Reputation: 1105
Add:
android:gravity="center_horizontal"
To your horisontal linear layout, the one which contains your two buttons.
The "android:layout_gravity" decides how the the view should position itself in it's parent, while "android:gravity" decides how the views children should be aligned
Upvotes: 4
Reputation: 3847
You can't really center anything unless it's parent is bigger than it. You have the parent to the buttons as wrap_content, so there's no room to center... try this for the buttons:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/registerGameButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_marginBottom="5sp"
android:layout_marginLeft="5sp"
android:layout_marginRight="2sp"
android:layout_marginTop="0sp" />
<Button
android:id="@+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close"
android:layout_marginBottom="5sp"
android:layout_marginLeft="2sp"
android:layout_marginRight="5sp"
android:layout_marginTop="0sp"/>
</LinearLayout>
Upvotes: 6