2

I have a Button with its width as match_parent. And I want a image/icon with a text in its center. I have triedandroid:drawableStart attribute but its of no help. I don't know what I am missing. Is there any one who is also facing the same problem.

Note:This link is not solving my problem. I am attaching my button portion of xml

<Button
    android:background="@drawable/blue_button"
    android:id="@+id/btn_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:drawableStart="@drawable/ic_list"
    android:text="@string/btn_schedule"
    android:textColor="@android:color/white" />

After adding these attribute my text is coming on center but image/icon is still on left side of button. Can any body help!!!

Community
  • 1
  • 1
Android
  • 3,828
  • 9
  • 46
  • 79

5 Answers5

1

You might want to check my answer to this question: Android Button with text and image

Its an ugly solution but it you want the image and text to be centered as a group in the button rather than text center and drawable padded from the side then its the only way I have found.

Community
  • 1
  • 1
Corey Scott
  • 2,430
  • 3
  • 28
  • 33
  • Using that will mend the default behavior, and I kindda do that as my last option available. I want to stick with android default behavior unless I am left with no other option then to create my own widget. – Android Jul 08 '13 at 06:30
0

I had the same problem and I hadn't found anywhere clear solution. BUT I got some information which also help me to provide following solutions (I have chosen the second one):

  1. Create image which will contain your image and text

     <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:drawableTop="@drawable/YOUR_IMAGE"
         android:gravity="center_horizontal|center_vertical" />
    
  2. It is not so clear and you have to add more complicated changes to your layout but it is easier to modify the text later

     <FrameLayout
         android:gravity="center_horizontal"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" >
    
         <Button
             android:layout_height="50dp"
             android:layout_width="match_parent" /> 
    
         <TextView
             android:text="sample"
             android:layout_height="50dp"
             android:layout_width="wrap_content"
             android:drawableLeft="@drawable/YOUR_IMAGE"
             android:gravity="center_horizontal|center_vertical" />
     </FrameLayout>
    
Arkadiusz Cieśliński
  • 5,307
  • 3
  • 23
  • 19
0

100% working

<FrameLayout
style="?android:attr/buttonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
    style="?android:attr/buttonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@null"
    android:clickable="false"
    android:drawableLeft="@android:drawable/ic_delete"
    android:focusable="false"
    android:gravity="center"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:text="Button Challenge" />

from here

Dude
  • 887
  • 6
  • 15
0

This works for me. Basically play with the paddingLeft and PaddingRight of the button to get the text and image close together to the center.

Note: Might be an issue with rather long texts.

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="search button"
            android:drawableStart="@drawable/ic_action_search"
            android:paddingLeft="60dp"
            android:paddingRight="60dp"/>
    </LinearLayout> 
irobotxx
  • 5,963
  • 11
  • 62
  • 91
-1

I tried android:drawableTop

The image shown in the center of the button

Sieryuu
  • 1,510
  • 2
  • 16
  • 41