Left align text inside a button in Android
JavaAndroidXmlJava Problem Overview
I want to align text of a button to the left, I don't know how to do this, please help me how to do this in the xml file. I didn´t find the properties for this.
Java Solutions
Solution 1 - Java
Maybe this will help you:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:text="Button" />
</LinearLayout>
Solution 2 - Java
You probably want both
android:gravity="left|center_vertical"
and then a little bit of space
android:paddingLeft="5dp"
to keep the text from running up against the left edge of the button.
(Adjust the amount of padding_left as appropriate for your button art.)
Solution 3 - Java
android:gravity="left"
Hope This will help
Solution 4 - Java
android:gravity="left|center_vertical"
It will work but now you may need to add some padding left according to your requirement.
android:paddingLeft="10dp"
Solution 5 - Java
You probably need BOTH the android:gravity
AND the android:layout_gravity
to align text to "left".
Solution 6 - Java
This is combination from above that worked for me, icon on left and text left aligned with some padding
<Button
android:id="@+id/butt_labs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/dt_labjob"
android:paddingLeft="10dip"
android:gravity="left|center_vertical"
android:text="@string/l_labs" />
Solution 7 - Java
As everyone else has mentioned, "left|center_vertical"
works, but I have found that if left or right is not explicitly stated, most properties default to left.
So putting the following should be enough to get your text left-justified and vertically centered:
android:gravity="center_vertical"
Solution 8 - Java
If you have to support many different languages it is better to use start
or end
to position text. That way for right to left (RTL) languages it will still work.
Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="start|center_vertical"
android:text="Button" />
</LinearLayout>