Example usage for android.content Context getResources

List of usage examples for android.content Context getResources

Introduction

In this page you can find the example usage for android.content Context getResources.

Prototype

public abstract Resources getResources();

Source Link

Document

Returns a Resources instance for the application's package.

Usage

From source file:Main.java

public static String getResourceString(String name, Context context) {
    int nameResourceID = context.getResources().getIdentifier(name, "string",
            context.getApplicationInfo().packageName);
    if (nameResourceID == 0) {
        throw new IllegalArgumentException("No resource string found with name " + name);
    } else {//from   w  ww  . j a  v  a2 s  .  c om
        return context.getString(nameResourceID);
    }
}

From source file:Main.java

public static int dip2px(Context context, float f) {
    return (int) (0.5F + f * context.getResources().getDisplayMetrics().density);
}

From source file:Main.java

public static int dip2px(Context ctx, float dpValue) {
    final float scale = ctx.getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
}

From source file:Main.java

public static int getNavigationBarHeight(Context context) {
    Resources resources = context.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        return resources.getDimensionPixelOffset(resourceId);
    }//from  w  w w  .  ja v a 2  s.  c  om
    return 0;
}

From source file:Main.java

public static int getNaviBarHeight(Context context) {
    Resources resources = context.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId);
    }//from ww w  .j av  a2 s  .co m
    return 0;
}

From source file:Main.java

public static int dip2px(Context context, int dipValue) {
    float density = context.getResources().getDisplayMetrics().density;
    return (int) (dipValue * density + 0.5f);
}

From source file:Main.java

public static int px2dp(Context ctx, float pxValue) {
    final float scale = ctx.getResources().getDisplayMetrics().density;
    return (int) (pxValue * scale + 0.5f);
}

From source file:Main.java

public static int px2dp(Context context, float px) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (px / scale + 0.5f);
}

From source file:Main.java

public static int pxToDp(Context context, float pxValue) {
    float density = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / density);
}

From source file:Main.java

public static float convertPxToDp(Context context, float px) {
    Resources resources = context.getResources();
    return px / (resources.getDisplayMetrics().densityDpi / 160f);
}