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.
In android, we can add an image to the button by using <ImageButton> attribute android:src in XML layout file or by using the setImageResource() method. In android, we can create ImageButton control in two ways either in the XML layout file or create it in the Activity file programmatically.
To make a View clickable so that users can tap (or click) it, add the android:onClick attribute in the XML layout and specify the click handler. For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.
We can add an image to the button simply by using attribute android:src in activity_main. xml file or by using setImageResource() method. In android, we can create ImageButton control in two ways either manually or programmatically.
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:background="@drawable/home_button"
android:drawableLeft="@android:drawable/ic_menu_edit"
android:drawablePadding="6dp"
android:gravity="left|center"
android:height="60dp"
android:padding="6dp"
android:text="AndroidDhina"
android:textColor="#000"
android:textStyle="bold" />
This can be done using the android:drawableTop
, android:drawableBottom
, android:drawableLeft
, android:drawableRight
attributes in your layout.xml.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With