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 Point getScreenMetrics(Context context) {
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    int screenWidth = dm.widthPixels;
    int screenHeight = dm.heightPixels;
    return new Point(screenWidth, screenHeight);

}

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) {
        return ""; //throw new IllegalArgumentException("No resource string found with name " + name);
    } else {/*from  w  ww  .  ja  v  a  2s.c  o m*/
        return context.getString(nameResourceID);
    }
}

From source file:Main.java

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

From source file:Main.java

public static float pxFromDp(final Context context, final float dp) {
    return dp * context.getResources().getDisplayMetrics().density;
}

From source file:Main.java

public static int px2sp(Context paramContext, float paramFloat) {
    float f = paramContext.getResources().getDisplayMetrics().scaledDensity;
    return (int) (paramFloat / f + 0.5F);
}

From source file:Main.java

/**
 * Change the density independent pixel to pixel according density
 *
 * @param dp//  w  w  w. j av a  2 s .  c  om
 * @return
 */
public static int dpToPx(Context context, int dp) {
    return (int) (dp * context.getResources().getDisplayMetrics().density);
}

From source file:Main.java

public static void showToast(int stringResId, Context context) {
    showToast(context.getResources().getString(stringResId), context);
}

From source file:Main.java

public static int px2dip(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 dpToPx(Context context, float dpValue) {
    float density = context.getResources().getDisplayMetrics().density;

    return (int) (density * dpValue + 0.5f);
}

From source file:Main.java

public static int dip2px(Context c, int dipValue) {
    float scale = c.getResources().getDisplayMetrics().density;
    if (scale == 0.0F)
        System.out.println("[scale] : 0");
    System.out.println("[scale] :  " + scale);
    System.out.println("[0.5f] :  0.5");
    System.out.println("[dipValue * scale + 0.5f] :  " + dipValue * scale + 0.5F);
    return (int) (dipValue * scale + 0.5F);
}