get Res Id from resource type, name and package name - Android App

Android examples for App:Resource

Description

get Res Id from resource type, name and package name

Demo Code


//package com.java2s;

import java.lang.reflect.Field;

public class Main {
    private static String mPackageName = null;

    public static Integer getResId(String rType, String rName,
            String packagename) {
        Object localObject = null;
        ;/*w ww  .java 2 s  .com*/
        try {
            Class localClass = Class.forName(packagename + ".R" + "$"
                    + rType);
            Field localField = localClass.getField(rName);
            localObject = localField.get(localClass.newInstance());
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return Integer.valueOf(Integer.parseInt(localObject.toString()));
    }

    public static Integer getResId(String rType, String rName) {
        return getResId(rType, rName, mPackageName);
    }
}

Related Tutorials