John x
John x

Reputation: 4031

Center Allign Text on Button

I have a button like

<Button
            android:id="@+id/btn_someid"
            android:layout_width="80dp"
            android:layout_height="50dp"
            android:layout_marginRight="5dp"
            android:shadowColor="#e1e1e1"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="5"
            android:background="@color/bstate"
            android:enabled="false"
            android:gravity="center"

            android:text="@string/ABCDEFG"
            android:textColor="@color/btstate"
            android:textSize="16sp" />

and it is appearing like

enter image description here

how can i make the text center align

Regards

Upvotes: 0

Views: 153

Answers (4)

John x
John x

Reputation: 4031

i just cannot believe it, after hours of hair pulling i closed the eclipse went out came back started eclipse and things were just as they were supposed to be that is text was center alligned.

Upvotes: 0

Piyush
Piyush

Reputation: 18923

If your button is in LinearLayout then just set gravity center to your button.

android:gravity="center

Upvotes: 1

No_Rulz
No_Rulz

Reputation: 2719

Your code works perfectly, try to change your gravity like

<Button
            android:id="@+id/select"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/btn_selectcontact_title"
            android:background="@color/button_bg_state"
            android:textColor="@color/button_state"
            android:gravity="center_horizontal|center_vertical" --> similar to center
            />

Upvotes: 2

Nambi
Nambi

Reputation: 12042

add this to your button android:gravity="right|center_vertical"

<Button
            android:id="@+id/select"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/btn_selectcontact_title"
            android:background="@color/button_bg_state"
            android:textColor="@color/button_state"
            android:gravity="right|center_vertical <!-- <HERE -->
            />

Upvotes: 1

Related Questions