Question

I have been using the pt unit in XML files. Now I need to set the width of an element at runtime. How can I set the width by points so that it is the same unit I have been using everywhere else. I assume I will need to multiply by resolution and dpi. A code sample would be best.

Was it helpful?

Solution

First you should really read the following in-depth article from the Android Developer Documentation :

http://developer.android.com/guide/practices/screens_support.html

Right in the middle you'll find the following under the title :

Do not use hard-coded pixel values in your code

// Convert the dps to pixels
final float scale = getContext().getResources().getDisplayMetrics().density;
mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);

OTHER TIPS

You can use dip instead of pt

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top