How can I convert String to Drawable

31,693

Solution 1

This can be done using reflection:

String name = "your_drawable";
final Field field = R.drawable.getField(name);
int id = field.getInt(null);
Drawable drawable = getResources().getDrawable(id);

Or using Resources.getIdentifier():

String name = "your_drawable";
int id = getResources().getIdentifier(name, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);

Then use this for setting the drawable in either case:

view.setBackground(drawable)

Solution 2

int resId = getResources().getIdentifier("your_drawable_name","drawable",YourActivity.this.getPackageName());
Drawable d = YourActivity.this.getResources().getDrawable(resId);

Solution 3

It can be done like this:

ImageView imageView = new ImageView(this);
imageView.setBackground(getResources().getDrawable(getResources().getIdentifier("name","id",getPackageName())));

Solution 4

Try this:

public Bitmap getPic (int number)
{
    return
        BitmapFactory.decodeResource
        (
            getResources(), getResourceID("myImage_" + number, "drawable", getApplicationContext())
        );
}

protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
    final int ResourceID =
        ctx.getResources().getIdentifier(resName, resType,
            ctx.getApplicationInfo().packageName);
    if (ResourceID == 0)
    {
        throw new IllegalArgumentException
        (
            "No resource string found with name " + resName
        );
    }
    else
    {
        return ResourceID;
    }
}

Solution 5

if you have the filename as string you can use:

int id = getResources().getIdentifier("name_of_resource", "id", getPackageName());

with this id you can access it like always (assumed its a drawable):

Drawable drawable = getResources().getDrawable(id);
Share:
31,693
Utku Soytaş
Author by

Utku Soytaş

http://iamutku.com

Updated on July 09, 2022

Comments

  • Utku Soytaş
    Utku Soytaş almost 2 years

    I have many icon in drawable folder and I have their name as String. How can I access to drawable folder and change background imageView (or any view) use these name in dynamically. Thanks

  • Utku Soytaş
    Utku Soytaş about 10 years
    Should I use view.setBackground(drawable) or view.setBackgroundResource(id) or what?
  • Utku Soytaş
    Utku Soytaş about 10 years
    Thanks a lot. That's work :) All answers are correct
  • Utku Soytaş
    Utku Soytaş about 10 years
    Thanks. I am sure it's true but I didn't try it :)
  • FD_
    FD_ about 10 years
    Glad I could help. I upvoted most of the other answers to compensate for the accept.
  • Utku Soytaş
    Utku Soytaş about 10 years
    Thanks :) I can't this because of my reputation :)
  • Costas Vrahimis
    Costas Vrahimis about 9 years
    Would anyone have any idea why I would have this problem: String name = "your_drawable"; int id = getResources().getIdentifier(name, "drawable", getPackageName()); Works and id gets an int value that seems right but: Drawable drawable = getResources().getDrawable(id); is always null?
  • Michal
    Michal almost 9 years
    This didn´t worked for me, but I found solution here: stackoverflow.com/questions/13351003/find-drawable-by-string
  • Dmitry
    Dmitry over 5 years
    I get error: Can't resolve name getPackageName()).
  • Dmitry
    Dmitry over 5 years
    It should be replaced by: getContext().getPackageName().
  • FD_
    FD_ over 5 years
    @Dmitry you should know how to obtain a context. My code assumes it is used directly inside a subclass of Context, such as an Activity or Service.