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.
Asked
Active
Viewed 6,619 times
-1
-
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 Answers
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" />
-
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"
-
https://meta.stackexchange.com/questions/88541/how-to-write-code-in-a-question-or-answer READ THIS TO WRITE YOUR FIRST QUESTION – Kousei Mar 16 '18 at 05:34
-