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

private static float convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return px;// w w  w.j a v  a2s.  c o  m
}

From source file:Main.java

public static boolean isPortrait(Context context) {
    return (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
}

From source file:Main.java

public static boolean isEN(Context context) {
    Locale locale = context.getResources().getConfiguration().locale;
    String language = locale.getLanguage();
    if (language.endsWith("en")) {
        return true;
    }/* ww  w  .j a  v  a 2  s.  co m*/
    return false;
}

From source file:Main.java

public static int pix2dip(Context context, int pixs) {
    int densityDpi = context.getResources().getDisplayMetrics().densityDpi;
    return (pixs * 160) / densityDpi;
}

From source file:Main.java

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

From source file:Main.java

public static int dp2px(Context context, float dpValue) {
    float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5F);
}

From source file:Main.java

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

From source file:Main.java

public static float getRateDpToPixel(Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float rateDpToPixel = ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return rateDpToPixel;
}

From source file:Main.java

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

From source file:Main.java

/**
 * This method converts dp to pixels//from  ww  w.  j  a  v a  2 s  .  c om
 *
 * @param dp
 *         Oiringla dp
 * @param context
 *         App context
 *
 * @return Dp value in pixels
 */
public static int dp2Pixels(int dp, Context context) {
    Resources r = context.getResources();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}