get String Id from resource - Android App

Android examples for App:Resource

Description

get String Id from resource

Demo Code


//package com.java2s;

public class Main {
    public static int getStringId(String packageName, String name) {
        return getResourseIdByName(packageName, "string", name);
    }//  www.j  a v  a2 s. c  om

    public static int getResourseIdByName(String packageName,
            String className, String name) {
        int id = 0;
        try {
            Class desireClass = Class.forName(packageName + ".R$"
                    + className);
            if (desireClass != null)
                id = desireClass.getField(name).getInt(desireClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        return id;
    }
}

Related Tutorials