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?
Had similar issue but wanted to have center drawable with no text and no wrapped layouts. Solution was to create custom button and add one more drawable in addition to LEFT, RIGHT, TOP, BOTTOM. One can easily modify drawable placement and have it in desired position relative to text.
CenterDrawableButton.java
attrs.xml
usage
You could put the button over a LinearLayout
public class DrawableCenterTextView extends TextView {
}
This may be an old/closed thread but I've search everywhere didnt find something useful, until I decided to create my own solution. If theres anyone here trying to look for answer try this one, might save you a minute of thinking
Because Linear layout act as the container and the one who has the selector, when you click the entire linear layout it would look just how it should it be. if you want it to be both centered, use relative insteaf. If you want it centered horizontally change the orientation to horizontal.
Take note DO NOT forget to add android:clickable="true" to your main container (relative or linear) for the action to take place.
Again this may be old thread but may still help someone there.
-cheers hope it helps- happycodings.
Try to use this library
OmegaCenterIconButton
I adjust it by adding padding left and right as follows: