Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Convert points to pixels

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.

like image 716
700 Software Avatar asked Jan 20 '11 16:01

700 Software


2 Answers

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);
like image 192
Yahel Avatar answered Oct 07 '22 13:10

Yahel


You can use dip instead of pt

like image 43
Aman Alam Avatar answered Oct 07 '22 11:10

Aman Alam