Friday 3 February 2012

Display a icon/image on button using Java code and Xml code

The Method
Button.setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)
sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

Example:
Button imageButton = (Button)findViewById(R.id.imagebutton);
imageButton.setCompoundDrawablesWithIntrinsicBounds(
0, //left
0, //top
R.drawable.icon, //right
0); //bottom

XML Code:
Example:
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- I'm a Button -"
android:drawableRight="@drawable/icon"
/>

No comments:

Post a Comment