user1497577
user1497577

Reputation: 691

Left align text inside a button in Android

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.

Upvotes: 69

Views: 86805

Answers (8)

Tim
Tim

Reputation: 1826

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>

Upvotes: 1

benkc
benkc

Reputation: 3382

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.)

Upvotes: 52

Shahbaz Ahmed
Shahbaz Ahmed

Reputation: 469

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"

Upvotes: 6

juil
juil

Reputation: 2488

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"

Upvotes: 1

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

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>

Upvotes: 158

Adam
Adam

Reputation: 33146

You probably need BOTH the android:gravity AND the android:layout_gravity to align text to "left".

Upvotes: 2

PedroMorgan
PedroMorgan

Reputation: 936

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" />

Upvotes: 2

Sieryuu
Sieryuu

Reputation: 1531

android:gravity="left"

Hope This will help

Upvotes: 28

Related Questions