Left align text inside a button in Android

I want to align the button text on the left, I don’t know how to do this, please help me how to do this in an XML file. I did not find any properties for this.

+47
java android xml
Jul 05 '12 at 3:26
source share
6 answers

Perhaps 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> 
+129
Jul 05 2018-12-12T00:
source share

You probably want

  android:gravity="left|center_vertical" 

and then a little

  android:paddingLeft="5dip" 

so that the text does not work against the left edge of the button.

(Adjust the amount of padding_left to suit your button art.)

+34
Aug 22 '13 at 19:31
source share

android:gravity="left"

Hope this will help

+21
Jul 05 2018-12-12T00:
source share

You will probably need android:gravity and android:layout_gravity to align the text to "left".

+2
Feb 05 '13 at 11:50
source share

This is the combination on top that worked for me, the icon on the left and the text aligned on the left with some addition

 <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" /> 
+1
Sep 10 '13 at 23:01
source share

Like everyone else, "left|center_vertical" works, but I found that if it is not explicitly indicated to the left or right, most properties are left by default.

Therefore, in order for the text to be left aligned and vertically centered, it should be sufficient:

 android:gravity="center_vertical" 
0
Jun 23 '16 at 20:49
source share



All Articles