AMH
AMH

Reputation: 6451

Button cannot be set to the center

I have login screen ,I want to center the button so I used android:layout_centerHorizontal="true"

the layout xml is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:background="#DDDDDD"
    android:gravity="center_vertical|center_horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/loginlayoutborders"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            gra=""
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tv_url"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="URL:             "
                android:textColor="#444444"
                android:textSize="10pt" />

            <EditText
                android:id="@+id/et_url"
                android:layout_width="400dip"
                android:layout_height="50dip"
                android:layout_toRightOf="@id/tv_url"
                android:background="@android:drawable/editbox_background" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            gra=""
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tv_un"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="User Name:"
                android:textColor="#444444"
                android:textSize="10pt" />

            <EditText
                android:id="@+id/et_un"
                android:layout_width="400dip"
                android:layout_height="50dip"
                android:layout_alignTop="@id/tv_un"
                android:layout_toRightOf="@id/tv_un"
                android:background="@android:drawable/editbox_background" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tv_pw"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_un"
                android:text="Password:"
                android:textColor="#444444"
                android:textSize="10pt" />

            <EditText
                android:id="@+id/et_pw"
                android:layout_width="400dip"
                android:layout_height="50dip"
                android:layout_alignTop="@id/tv_pw"
                android:layout_below="@id/et_un"
                android:layout_marginLeft="17dip"
                android:layout_toRightOf="@id/tv_pw"
                android:background="@android:drawable/editbox_background"
                android:password="true" />
        </LinearLayout>

        <Button
            android:id="@+id/btn_login"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_below="@id/et_pw"
            android:layout_centerHorizontal="true"
            android:text="Login" />

        <TextView
            android:id="@+id/tv_error"
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:layout_below="@id/btn_login"
            android:layout_marginLeft="15dip"
            android:layout_marginRight="9dip"
            android:layout_marginTop="15dip"
            android:text=""
            android:textColor="#AA0000"
            android:textSize="7pt" />
    </LinearLayout>

</RelativeLayout>

and the style file is

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item>

    <shape>

        <gradient
            android:angle="90"
            android:startColor="#FFFFFF"
            android:endColor="#FFFFFF"
            android:type="linear" />

        <padding android:left="50dp" android:top="50dp" 
            android:right="50dp" android:bottom="50dp" /> 

        <corners android:radius="4dp" /> 

    </shape>

</item>

</selector>

but the button always be at the left of the screen, any idea how to fix that

Upvotes: 0

Views: 122

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

add android:layout_gravity to center for button

 <Button
            android:layout_gravity="center"
            android:id="@+id/btn_login"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_below="@id/et_pw"
            android:layout_centerHorizontal="true"
            android:text="Login" />
 </LinearLayout>

Upvotes: 2

WarrenFaith
WarrenFaith

Reputation: 57672

That is because your button is inside the LinearLayout. Anyway have a LinearLayout in a RelativeLayout as the only child doesn't really make sense.

If would recommend to build the complete Layout without the LinearLayouts.

Upvotes: 1

Related Questions