Android: How to reference a drawable image in XML (or Java)

When you need to reference a drawable image from an Android XML file, such as a layout or menu file, use this tag:

android:src="@drawable/myimage"

That assumes that you have a file named myimage.png in your res/drawable directories. As a more complete example, this shows how I reference an image named images_show.png in an Android menu item:

<item
    android:id="@+id/menu_item_control_images"
    android:icon="@drawable/images_show"
    android:title="@string/menu_hide_images"
    myapp:showAsAction="ifRoom"
    />

In your Android/Java source code you can also refer to that same image like this:

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.myimage);

For more information, see the Android.com Drawable Resources page.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.