I want to have an Android Button with icon+text centered inside it. I'm using the drawableLeft attribute to set the image, this works well if the button has a width of "wrap_content"
but I need to stretch to max width so I use width "fill_parent"
. This moves my icon straight to the left of the button and I want both icon and text centered inside the button.
I've try setting up the padding but this only allows to give a fixed value so it is not what I need. I need to have icon+text aligned in the center.
<Button
android:id="@+id/startTelemoteButton"
android:text="@string/start_telemote"
android:drawableLeft="@drawable/start"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:width="fill_parent"
android:heigh="wrap_content" />
Any suggestions on how I could achieve that?
Other possibility to keep Button theme.
With this solution if your add @color/your_color @color/your_highlight_color in your activity theme, you can have Matherial theme on Lollipop whith shadow and ripple, and for previous version flat button with your color and highlight coor when you press it.
Moreover, with this solution autoresize of picture
Result : First on Lollipop device Second : On pre lollipop device 3th : Pre lollipop device press button
This is my solution..
Put the FrameLayout in LinearLayout and set orientation to Horizontal.
I think android:gravity= "centre" should work
As suggested by Rodja, prior to 4.0 there isn't a direct way to center the text with the drawable. And indeed setting a padding_left value does move the drawable away from the border. Therefore my suggestion is that on runtime you calculate exactly how many pixels from the left border your drawable needs to be and then pass it using setPadding Your calculation may be something like
The width of your drawable's is fixed and you can look it up and you can also calculate or guess the text width.
Finally, you would need to multiple the padding value by the screen density, which you can do using DisplayMetrics
Use RelativeLayout (container) and android:layout_centerHorizontal="true" ,my sample: