Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android add image to button

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.

like image 213
stealthjong Avatar asked Sep 19 '12 09:09

stealthjong


People also ask

Can we add an image to a button in android?

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.

How do I make a clickable image button?

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.

How do I add an image button to Kotlin?

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.


2 Answers

enter image description here

     <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" />
like image 147
Dhina k Avatar answered Oct 28 '22 06:10

Dhina k


This can be done using the android:drawableTop, android:drawableBottom, android:drawableLeft, android:drawableRight attributes in your layout.xml.

like image 40
Ridcully Avatar answered Oct 28 '22 08:10

Ridcully