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

static int getResourceId(Context context, String name, String type, String packageName) {

    Resources themeResources = null;
    PackageManager pm = context.getPackageManager();
    try {/*from   w  ww .  j ava 2  s.c  o  m*/
        themeResources = pm.getResourcesForApplication(packageName);
        return themeResources.getIdentifier(name, type, packageName);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:despotoski.nikola.github.com.bottomnavigationlayout.Util.java

public static int getNavigationBarHeight(Context context) {
    Resources res = context.getResources();
    int navBarIdentifier = res.getIdentifier("navigation_bar_height", "dimen", "android");
    int navBarHeight = 0;
    if (navBarIdentifier > 0) {
        navBarHeight = res.getDimensionPixelSize(navBarIdentifier);
    }/*  w  ww.  j  av  a  2 s.c o m*/
    return navBarHeight;
}

From source file:Main.java

public static String resolveString(Context context, Resources res, String string) {
    if (string.startsWith("@")) {
        String subs = string.substring(1, string.length());
        String[] parts = subs.split("/");
        int id = res.getIdentifier(parts[1], parts[0], context.getPackageName());
        if (id == 0x0) {
            return string;
        }//from www . j a v  a 2  s  .  c  o m
        return res.getString(id);
    }
    return string;
}

From source file:Main.java

/**
 * Turns a string into an array of drawable resource IDs.
 * 'string' must contain string equivalent of valid drawable names without file endings
 * separated by commas./* w ww  .jav  a 2s  . c  o m*/
 * @param string
 * @param context
 * @return an array of drawable resource IDs
 */
public static int[] stringToDrawableResIDArray(String string, Context context) {
    String[] strArray = string.split(",");
    int[] intArray = new int[strArray.length];
    Resources resources;
    int resId;
    int i = 0;

    for (String str : strArray) {
        resources = context.getApplicationContext().getResources();
        intArray[i] = resources.getIdentifier(strArray[i], "drawable", "com.domnibus.sfarinas.youtubetest");
        i++;
    }

    return intArray;
}

From source file:com.chromium.fontinstaller.util.ViewUtils.java

public static int getStatusBarHeight(Context context) {
    final Resources resources = context.getResources();
    final int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
    return resourceId > 0 ? resources.getDimensionPixelSize(resourceId) : 0;
}

From source file:Main.java

public static int getNavigationBarHeight(Context c) {
    int result = 0;
    boolean hasMenuKey = ViewConfiguration.get(c).hasPermanentMenuKey();
    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

    if (!hasMenuKey && !hasBackKey) {
        //The device has a navigation bar
        Resources resources = c.getResources();

        int orientation = resources.getConfiguration().orientation;
        int resourceId;
        if (isTablet(c)) {
            resourceId = resources
                    .getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height"
                            : "navigation_bar_height_landscape", "dimen", "android");
        } else {/*from w w  w. ja va2 s .c o  m*/
            resourceId = resources
                    .getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height"
                            : "navigation_bar_width", "dimen", "android");
        }

        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        }
    }
    return result;
}

From source file:Main.java

/**
 * Get resource ID from extra or from metadata.
 * /* w w  w . j av  a2 s.  c  om*/
 * @param context
 * @param packagename
 * @param intent
 * @param extra
 * @param metadata
 * @return
 */
public static int getResourceIdExtraOrMetadata(final Context context, final String packagename,
        final Intent intent, final String extra, final String metadata) {
    if (intent.hasExtra(extra) && intent.getStringExtra(extra) != null) {

        int id = 0;
        try {
            String resourcename = intent.getStringExtra(extra);
            Resources resources = context.getPackageManager().getResourcesForApplication(packagename);
            Log.i(TAG, "Looking up resource Id for " + resourcename);
            id = resources.getIdentifier(resourcename, "", packagename);
        } catch (NameNotFoundException e) {
            Log.e(TAG, "Package name not found", e);
        }
        return id;
    } else {
        //Try meta data of package
        Bundle md = null;
        try {
            md = context.getPackageManager().getApplicationInfo(packagename,
                    PackageManager.GET_META_DATA).metaData;
        } catch (NameNotFoundException e) {
            Log.e(TAG, "Package name not found", e);
        }

        if (md != null) {
            // Obtain resource ID and convert to resource name:
            int id = md.getInt(metadata);

            return id;
        } else {
            return 0;
        }

    }
}

From source file:com.onesignal.OSUtils.java

static String getResourceString(Context context, String key, String defaultStr) {
    Resources resources = context.getResources();
    int bodyResId = resources.getIdentifier(key, "string", context.getPackageName());
    if (bodyResId != 0)
        return resources.getString(bodyResId);
    return defaultStr;
}

From source file:de.avpptr.umweltzone.utils.ContentProvider.java

@RawRes
private static int getRawResourceId(Context context, String fileName, String folderName) {
    final Resources resources = context.getResources();
    // Look-up identifier using reflection (expensive)
    int rawResourceId = resources.getIdentifier(fileName, folderName, context.getPackageName());
    if (rawResourceId == de.avpptr.umweltzone.contract.Resources.INVALID_RESOURCE_ID) {
        String filePath = getFilePath(folderName, fileName);
        Umweltzone.getTracker().trackError(TrackingPoint.ResourceNotFoundError, filePath);
        throw new IllegalStateException("Resource for file path '" + filePath + "' not found.");
    }/*ww  w. j a v a 2s. c om*/
    return rawResourceId;
}

From source file:Main.java

public static int resolveId(Context context, Resources res, String string) {
    if (string.startsWith("@")) {
        String subs = string.substring(1, string.length());
        String[] parts = subs.split("/");
        if (parts[0].startsWith("+"))
            parts[0] = parts[0].substring(1);
        return res.getIdentifier(parts[1], parts[0], context.getPackageName());

    }//from   w w w.j ava2s  . com
    return Integer.parseInt(string);
}