Java tutorial
//package com.java2s; import android.content.Context; public class Main { public static int getIdResource(Context ctx, String name) { String packageName = ctx.getPackageName(); try { Object obj; obj = Class.forName(packageName + ".R$id").newInstance(); return Class.forName(packageName + ".R$id").getDeclaredField(name).getInt(obj); } catch (Exception e) { e.printStackTrace(); return 0; } } }