6

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

  • I have two background images for button (button_off and button_on) first is for normal button and second is for onFocus / onPressed state.

enter image description here enter image description here

  • Now I have to put text on button then it should look like:

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.

Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
Saurabh Pareek
  • 7,126
  • 4
  • 28
  • 29
  • follow this link [Buttons](http://www.androidpeople.com/button) , u will get answer here. and surf through this website u will learn alot – Prachur Mar 11 '11 at 12:57
  • 2
    I can't find the answer on the linked page. And besides, I think it's bad style to post a link/article collection as an answer to a specific question. If the answer can be found somewhere among the articles, just link directly to the article, at least. – Hinton Oct 20 '12 at 18:36

6 Answers6

3

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

`

3

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

sergeytch
  • 161
  • 7
  • But as you can see in that button image text is not in the center of Button. that is 5 pixel more then center. So how to do that? – Saurabh Pareek Mar 11 '11 at 12:52
  • When you use 9-patch as a background, you manually (with Google's "draw9patch" tool) define the area which is stretchable, and the area where your text will appear. I think it should solve your issue. – sergeytch Mar 11 '11 at 12:58
1
<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.

Ravi K. Sharma
  • 545
  • 8
  • 15
1

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

sat
  • 40,138
  • 28
  • 93
  • 102
  • we can use padding only for layout not for text. by setting gravity we can set only left/ right / top /bottom /center alignment. but i want to set text 5 pixel more then center.. – Saurabh Pareek Mar 11 '11 at 12:54
  • I am able to use padding even for text ! – sat Mar 11 '11 at 13:13
0

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
reixa
  • 6,903
  • 6
  • 49
  • 68
0

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>
GAAAN
  • 399
  • 2
  • 3
  • 14