get Int In Class R using reflection - Android App

Android examples for App:R

Description

get Int In Class R using reflection

Demo Code


//package com.java2s;
import java.lang.reflect.Field;

import android.content.Context;
import android.content.res.Resources.NotFoundException;

public class Main {

    public static int getIntInClassR(Context context, Class<?> classR,
            String filedName) {/*from  w w w  .  ja  v  a 2 s  .  c om*/
        if (classR != null) {
            try {
                Field intField = classR.getField(filedName);
                int sourceId = intField.getInt(intField);
                return sourceId;
            } catch (NoSuchFieldException e) {
            } catch (IllegalAccessException e) {
            } catch (IllegalArgumentException e) {
            } catch (NotFoundException e) {
            }
        }
        return -1;
    }
}

Related Tutorials