Skip to content

Instantly share code, notes, and snippets.

@fidanov
Created July 4, 2012 11:26
Show Gist options
  • Save fidanov/3046864 to your computer and use it in GitHub Desktop.
Save fidanov/3046864 to your computer and use it in GitHub Desktop.
Android: DIP 2 PX programmatically. Works on any device with any density. Useful when settings width and height in code.
import android.content.Context;
import android.util.TypedValue;
public class Util {
public static int dp(Context context, int size) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
size,
context.getResources().getDisplayMetrics());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment