Saurabh Pareek
Saurabh Pareek

Reputation: 7134

Alignment of text on button

I am creating a custom button. I want to set the position of text on button.

enter image description here enter image description here

enter image description here enter image description here

But i am unable to set the exact position of the text on this background button image. Please share your suggestion to solve this issue.

Upvotes: 6

Views: 22556

Answers (6)

GAAAN
GAAAN

Reputation: 399

You can use the layout as a button style="@android:style/Widget.Button", and configure it as you want.

Paste this in your xml file:

<LinearLayout
android:id="@+id/button"
style="@android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="2dp"
    android:text="TextView" />

</LinearLayout>

Upvotes: 0

habib ullah
habib ullah

Reputation: 93

You can set these properties in XML file, `

<Button
    android:id="@+id/button1"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:drawablePadding="5dp"
    android:drawableLeft="@android:drawable/ic_lock_lock"
    android:text="All" 
    android:gravity="center"/>

`

Upvotes: 3

Ravi K. Sharma
Ravi K. Sharma

Reputation: 545

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@android:drawable/arrow_down_float"
    android:background="@drawable/btn_all_bg"
    android:text="All">
 Button>

You can use the drawableLeft/right/top/bottom to add icon on a button and text would automatically adjust.

Upvotes: 1

user2144993
user2144993

Reputation:

Few ways :

  1. define a button and try android:drawableStart
  2. extend Button and draw yourself
  3. try to define a LinearLayout that hold a text and an image with a background of your button, set both of them weight of 1 and put them inside another LinearLayout and make them onClick can't assure 3 will work without a tweak or two but it worth trying

Upvotes: 0

sat
sat

Reputation: 41106

Align the text , use gravity option ! or try with padding

Upvotes: 1

sergeytch
sergeytch

Reputation: 161

You can try using 9-patch image as a background. More information on this here: http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

Upvotes: 3

Related Questions