get Drawable By Name - Android android.graphics.drawable

Android examples for android.graphics.drawable:Drawable

Description

get Drawable By Name

Demo Code

import android.content.Context;
import android.graphics.drawable.Drawable;

public class Main {

  public static Drawable getDrawableByName(String name, Context context) {
    int drawableResource = context.getResources().getIdentifier(name, "drawable", context.getPackageName());
    if (drawableResource == 0) {
      throw new RuntimeException("Can't find drawable with name: " + name);
    }//w  w  w  .j  ava  2 s  .co  m
    return context.getResources().getDrawable(drawableResource);
  }

}

Related Tutorials