I want to add an image to a Button
(no imagebutton
, as the particular button can either contain text or an image; plus the imagebutton has the same problem), while still retaining the original android-style button. So adding android:background
in the XML doesn't cut it, as that will remove the android default button with rounded corners etc. (android.R.drawable.btn_default)
Is this in any way possible?
I think one way is to make a 9patch
image for the button pressed (one for up and one for down) and onTouch Action_DOWN
make a layered background drawable and put that onto the button, and doing the same thing but with another 9patch
for onTouch Action_UP
, but I think that will decrease the performance of the application substantially, as that will require quite a lot of resource reading and layer merging for all the button clicks (and for my application, that will be quite a lot). Is what I state above correct?
EDIT: I can't declare the source of the image in the XML, because I get the images from a web service, so anything can be put on the Buttons, but it has to happen programmatically.
This can be done using the
android:drawableTop
,android:drawableBottom
,android:drawableLeft
,android:drawableRight
attributes in your layout.xml.You can create your own drawable with multiple states and set that to background
Drawables can have rounded corners like so.
For setting Image :
For setting text :
Code :
for more info http://androiddhina.blogspot.in/2015/09/how-to-add-image-inside-button.html
Set below Property of Button for Display Image with Text, using this property image is displaying above text.