What is the correct way to specify dimensions in DIP from Java code?

There is an existing utility method built called TypedValue.applyDimensions(int, float, DisplayMetrics) that does this. Here’s how to use it: // returns the number of pixels for 123.4dip int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) 123.4, getResources().getDisplayMetrics()); There’s a list of other types that you can use with it including COMPLEX_UNIT_SP, COMPLEX_UNIT_PT all found in the page … Read more

How do you make layouts for several Android screen sizes?

There are two axes to consider when it comes to screen size: physical size and density. Density is handled by providing measurements in dips and scaled resources as appropriate. But density does not always imply size or vice versa. See http://developer.android.com/guide/practices/screens_support.html for some further info on the mechanics. It is not common or recommended to … Read more

Convert dip to px in Android

Try this: Java public static float dipToPixels(Context context, float dipValue) { DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics); } Kotlin fun Context.dipToPixels(dipValue: Float) = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, resources.displayMetrics)

Does setWidth(int pixels) use dip or px?

It uses pixels, but I’m sure you’re wondering how to use dips instead. The answer is in TypedValue.applyDimension(). Here’s an example of how to convert dips to px in code: // Converts 14 dip into its equivalent px Resources r = getResources(); int px = Math.round(TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 14,r.getDisplayMetrics()));

How to determine the screen width in terms of dp or dip at runtime in Android?

As @Tomáš Hubálek mentioned; Try something like: DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); float dpHeight = displayMetrics.heightPixels / displayMetrics.density; float dpWidth = displayMetrics.widthPixels / displayMetrics.density; OR Try old answer: Display display = getWindowManager().getDefaultDisplay(); DisplayMetrics outMetrics = new DisplayMetrics (); display.getMetrics(outMetrics); float density = getResources().getDisplayMetrics().density; float dpHeight = outMetrics.heightPixels / density; float dpWidth = outMetrics.widthPixels / density;

How to convert DP, PX, SP among each other, especially DP and SP?

DP to PX: public static int dpToPx(float dp, Context context) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); } SP to PX: public static int spToPx(float sp, Context context) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics()); } DP to SP: public static int dpToSp(float dp, Context context) { return (int) (dpToPx(dp, context) / context.getResources().getDisplayMetrics().scaledDensity); }