Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set image on a button in android?

Tags:

android

button

i have been trying to set a image on a button using the following code but it doesn't seem to work...i think what i am doing wrong is the path of the image i am using but i tried different paths and it wont work...i have copied my image to the drawable folder in res folder...what am i doing wrong here??

final Button next = (Button) findViewById(R.id.Button02) ;
 Drawable d = Drawable.createFromPath("@drawable/finalarrow1");
  next.setBackgroundDrawable(d);
like image 480
hemant Avatar asked Sep 14 '10 12:09

hemant


People also ask

How will you add graphics to button?

Copy your image file within the Res/drawable/ directory of your project. While in XML simply go into the graphic representation (for simplicity) of your XML file and click on your ImageButton widget that you added, go to its properties sheet and click on the [...] in the src: field. Simply navigate to your image file.

How do I put text on an image button?

You cannot set text to ImageButton because it has no method as setText() or android:text property. ImageButtons can't have text (or, at least, android:text isn't listed in its attributes). It looks like you need to use Button (and look at drawableTop or setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)) .

What is an image button?

In Android, ImageButton is used to display a normal button with a custom image in a button. In simple words we can say, ImageButton is a button with an image that can be pressed or clicked by the users.


1 Answers

Why not use

final Button next = (Button) findViewById(R.id.Button02);
next.setBackgroundResource(R.drawable.finalarrow1);
like image 149
Kan-Ru Chen Avatar answered Sep 22 '22 03:09

Kan-Ru Chen