-1

I am trying to add an image to a button(image button or normal button) but not in a specific res/drawable folder.
I want this image as a ImageView object or drawable object etc, but I couldn't.

Andrei T
  • 2,985
  • 3
  • 21
  • 28
Enes
  • 89
  • 1
  • 2
  • 8
  • I'm sorry, I did not understood your question clearly. Could you explain it again please. – Varun Kumar Aug 03 '16 at 02:52
  • ok thanks. Here is what I want to do. I have a button and I want to put image object on it. For example in java language, there is setIcon() method and you can put icon object but in android there is not. – Enes Aug 03 '16 at 03:36

2 Answers2

6

You can set an icon for Button by adding following attribute in XML for Button like this:

android:drawableLeft="@drawable/button_icon"

If you want to know how to do it programmatically, follow this article: How to programmatically set drawableLeft on Android button?

Or just add 'background':

<Button
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/qrcode"/>

But i think it's better if you use ImageButton, see this example:

<ImageButton
    android:id="@+id/searchImageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/your_image_drawable" />
Community
  • 1
  • 1
xxx
  • 3,315
  • 5
  • 21
  • 40
  • For the ImageButton, if you want the image to fill a button of a given size you can also set the size of the button and then use android:scaleType="fitXY" to fill it. Adding some padding allows it not quite fill to the edge also. – Mick Apr 06 '21 at 10:52
-1

Use ImageButton and add src in your background:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/yourImageNameHere"
timiTao
  • 1,417
  • 3
  • 20
  • 34