get Drawable Resource - Android App

Android examples for App:Resource

Description

get Drawable Resource

Demo Code


//package com.java2s;
import android.content.Context;

public class Main {

    public static int getDrawableResource(Context ctx, String name) {
        String packageName = ctx.getPackageName();
        try {//from  w w  w . j  a  v  a 2s.c  o  m
            Object obj;
            obj = Class.forName(packageName + ".R$drawable").newInstance();
            return Class.forName(packageName + ".R$drawable")
                    .getDeclaredField(name).getInt(obj);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}

Related Tutorials