Using Drawable Resources

Using drawable resources

You cant have an xml drawable as source for bitmap. Because for example if it was possible, then it could mistakenly create a black-hole by calling xml to itself.

Lets suppose, you have an xml drawable A which has a bitmap whos source is drawable B. But in drawable B, it has a bitmap whos source is drawable A. This will create a circular loop which cant be resolved. That is why you need to provide an image as a source for bitmap to avoid any confusion

how to use drawable resource in background

Instead of using android:color="#FF0000FF" inside the item you need to use android:drawable

and you need to create a drawable with the solid background you are aiming for.

So it would be something like

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/button_bg_press" />
<item android:state_focused="true" android:drawable="@drawable/button_bg_press" />
<item android:state_pressed="true" android:drawable="@drawable/button_bg_press" />

and the background drawable something like:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF4081"/>
</shape>

how to access the drawable resources by name in android

Note that your approach is almost always the wrong way to do things (better to pass the context into the object itself that is using the drawable than keeping a static Context somewhere).

Given that, if you want to do dynamic drawable loading, you can use getIdentifier:

Resources resources = context.getResources();
final int resourceId = resources.getIdentifier(name, "drawable",
context.getPackageName());
return resources.getDrawable(resourceId);

Drawable resource using a variable

You can use the name of a resource like

getIdentifier (String name, String defType, String defPackage);

getResources().getIdentifier("us","drawable","com.app");

The above function will return an integer value same as R.drawable.us.

This is how you access with resource names.

Using parents for drawable resources

Short answer - you cannot extend and override drawables. However,

  • Some drawables can be created from other drawables. e.g. layer-list will allow you to draw layers of drawables one on top of another. Refer to http://developer.android.com/guide/topics/resources/drawable-resource.html .
  • You create Drawable objects in code.

How to create Drawable from resource

Your Activity should have the method getResources. Do:

Drawable myIcon = getResources().getDrawable( R.drawable.icon );



As of API version 21 this method is deprecated and can be replaced with:

Drawable myIcon = AppCompatResources.getDrawable(context, R.drawable.icon);

If you need to specify a custom theme, the following will apply it, but only if API is version 21 or greater:

Drawable myIcon =  ResourcesCompat.getDrawable(getResources(), R.drawable.icon, theme);

Button background not set correctly on Android button using drawable resource

The issue was solved by using the Theme.AppCompat.DayNight theme.

Retrieving all Drawable resources from Resources object

If you find yourself wanting to do this you're probably misusing the resource system. Take a look at assets and AssetManager if you want to iterate over files included in your .apk.



Related Topics



Leave a reply



Submit