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 android.content.Context;

public class Main {
    /** return the res id of android ( current only the calling package )*/
    public static int getResId(String str, Context context) {
        String packageName;
        if (str.startsWith("android.R.")) {
            packageName = "android";
            //android.R.anim.xxx -> R.anim.xxx
            str = str.substring(str.indexOf(".") + 1);
        } else {
            packageName = context.getApplicationInfo().packageName;
        }
        //final String packageName = context.getApplicationInfo().packageName;
        final String[] strs = str.split("\\.");
        try { //R.drawable.xxx_xx
            return Class.forName(packageName + ".R$" + strs[1]).getField(strs[2]).getInt(null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}