Example usage for android.content.res Resources getIdentifier

List of usage examples for android.content.res Resources getIdentifier

Introduction

In this page you can find the example usage for android.content.res Resources getIdentifier.

Prototype

public int getIdentifier(String name, String defType, String defPackage) 

Source Link

Document

Return a resource identifier for the given resource name.

Usage

From source file:Main.java

/**
 * Returns the string corresponding to a resource name in a specific package.
 *
 * @param context The parent context./*from ww  w  . jav  a  2 s.  c o m*/
 * @param packageName The application's package name, e.g. "com.android.settings".
 * @param resName The short name of the resource, e.g. "ok".
 * @return A package-specific string, or {@code null}.
 */
public static String getPackageString(Context context, String packageName, String resName) {
    final PackageManager pm = context.getPackageManager();

    final Resources packageRes;
    try {
        packageRes = pm.getResourcesForApplication(packageName);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }

    final int resId = packageRes.getIdentifier(resName, "string", packageName);
    if (resId <= 0) {
        return null;
    }

    final String result;
    try {
        result = packageRes.getString(resId);
    } catch (Resources.NotFoundException e) {
        e.printStackTrace();
        return null;
    }

    return result;
}

From source file:com.google.android.gms.common.internal.C1339l.java

private static String m4743a(Context context, String str) {
    synchronized (f3100a) {
        String str2 = (String) f3100a.get(str);
        if (str2 != null) {
            return str2;
        }//from   ww w.  ja  va  2  s  . c om
        Resources b = C1296d.m4596b(context);
        if (b == null) {
            return null;
        }
        int identifier = b.getIdentifier(str, "string", "com.google.android.gms");
        if (identifier == 0) {
            String str3 = "GoogleApiAvailability";
            String str4 = "Missing resource: ";
            str2 = String.valueOf(str);
            Log.w(str3, str2.length() != 0 ? str4.concat(str2) : new String(str4));
            return null;
        }
        Object string = b.getString(identifier);
        if (TextUtils.isEmpty(string)) {
            str3 = "GoogleApiAvailability";
            str4 = "Got empty resource: ";
            str2 = String.valueOf(str);
            Log.w(str3, str2.length() != 0 ? str4.concat(str2) : new String(str4));
            return null;
        }
        f3100a.put(str, string);
        return string;
    }
}

From source file:com.bobomee.android.common.util.ScreenUtil.java

/**
 * get the height of NavigationBar//from   w  w  w  . ja v a2 s.co  m
 */

public static int getNavigationBarHeight(Activity mActivity) {
    Resources resources = mActivity.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    int height = resources.getDimensionPixelSize(resourceId);
    return height;
}

From source file:com.xxxifan.devbox.core.util.ViewUtils.java

private static int readInternalDimen(String key, Resources res, int fallback) {
    int resourceId = res.getIdentifier(key, "dimen", "android");
    return resourceId > 0 ? res.getDimensionPixelSize(resourceId) : fallback;
}

From source file:com.xxxifan.devbox.core.util.ViewUtils.java

private static boolean readInternalBoolean(String key, Resources res, boolean fallback) {
    int resourceId = res.getIdentifier(key, "bool", "android");
    return resourceId != 0 ? res.getBoolean(resourceId) : fallback;
}

From source file:com.simas.vc.helpers.Utils.java

public static int getNavigationBarHeight() {
    Resources resources = VC.getAppContext().getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId);
    }// www .  java 2 s. co  m
    return 0;
}

From source file:com.taobao.weex.utils.ImgURIUtil.java

public static Drawable getDrawableFromLoaclSrc(Context context, Uri rewrited) {
    Resources resources = context.getResources();
    List<String> segments = rewrited.getPathSegments();
    if (segments.size() != 1) {
        WXLogUtils.e("Local src format is invalid.");
        return null;
    }//from  w  w  w  . j  a va  2s . c  o m
    int id = resources.getIdentifier(segments.get(0), "drawable", context.getPackageName());
    return id == 0 ? null : ResourcesCompat.getDrawable(resources, id, null);
}

From source file:run.ace.NativeHost.java

public static int getResourceId(String name, String type, Context context) {
    String packageName = ((Application) context.getApplicationContext()).getPackageName();
    Resources resources = ((Application) context.getApplicationContext()).getResources();
    int id = resources.getIdentifier(name, type, packageName);
    if (id == 0) {
        throw new RuntimeException("Resource '" + packageName + ":" + type + "/" + name + "' not found");
    }//from ww w. j  a v a2 s .  c o  m
    return id;
}

From source file:com.cardvlaue.sys.util.ScreenUtil.java

public static void statusBarHeight(Resources resources, View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            int statusBarHeight = resources.getDimensionPixelSize(resourceId);
            if (statusBarHeight > 0) {
                view.setLayoutParams(//w ww .j  a va2 s. c o  m
                        new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, statusBarHeight));
            }
        }
    } else {
        view.setVisibility(View.GONE);
    }
}

From source file:ca.frozen.curlingtv.classes.Utils.java

public static int getNavigationBarHeight(Context context, int orientation) {
    Resources resources = context.getResources();
    String name = orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height"
            : "navigation_bar_height_landscape";
    int id = resources.getIdentifier(name, "dimen", "android");
    return (id > 0) ? resources.getDimensionPixelSize(id) : 0;
}