Left align text inside a button in Android

JavaAndroidXml

Java 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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1497577View Question on Stackoverflow
Solution 1 - JavaMohsin NaeemView Answer on Stackoverflow
Solution 2 - JavabenkcView Answer on Stackoverflow
Solution 3 - JavaSieryuuView Answer on Stackoverflow
Solution 4 - JavaShahbaz AhmedView Answer on Stackoverflow
Solution 5 - JavaAdamView Answer on Stackoverflow
Solution 6 - JavaPedroMorganView Answer on Stackoverflow
Solution 7 - JavajuilView Answer on Stackoverflow
Solution 8 - JavaTimView Answer on Stackoverflow