Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

public class Main {
    /**
     * Gets resource ID of the resource
     * @param resourceName  resource name
     * @param clazz an instance of Class, e.g. R.drawable.class
     * @return  an resource ID
     */
    public static int getId(String resourceName, Class<?> clazz) {
        try {
            Field idField = clazz.getDeclaredField(resourceName);
            return idField.getInt(idField);
        } catch (Exception e) {
            throw new RuntimeException("No resource ID found for: " + resourceName + " / " + clazz, e);
        }
    }
}