Android Java determine pixel density in code
Here's an answer that will probably be helpful.
From the answers there it looks like this is the line you're looking for
DisplayMetrics dm = context.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
Best way to determine screen dimensions for signature capture stroke width?
public static int convertDpsToPixels(Context context, int dps) {
// http://developer.android.com/guide/practices/screens_support.html
// Convert the dps to pixels
final float scale = context.getResources().getDisplayMetrics().density;
final float dpsFloat = dps;
return (int) (dpsFloat * scale + 0.5f);
}
What is the difference between px, dip, dp, and sp?
From the Android Developer Documentation:
px
Pixels - corresponds to actual pixels on the screen.in
Inches - based on the physical size of the screen.
1 Inch OR 2.54 centimetersmm
> Millimeters - based on the physical size of the screen.pt
> Points - 1/72 of an inch based on the physical size of the screen.dp or dip
> Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160
dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".sp
> Scaleable Pixels OR scale-independent pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you
use this unit when specifying font sizes, so they will be adjusted
for both the screen density and the user's preference. Note, the Android documentation is inconsistent on whatsp
actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".
From Understanding Density Independence In Android:
Density Bucket | Screen Density | Physical Size | Pixel Size |
---|---|---|---|
ldpi | 120 dpi | 0.5 x 0.5 in | 0.5 in * 120 dpi = 60x60 px |
mdpi | 160 dpi | 0.5 x 0.5 in | 0.5 in * 160 dpi = 80x80 px |
hdpi | 240 dpi | 0.5 x 0.5 in | 0.5 in * 240 dpi = 120x120 px |
xhdpi | 320 dpi | 0.5 x 0.5 in | 0.5 in * 320 dpi = 160x160 px |
xxhdpi | 480 dpi | 0.5 x 0.5 in | 0.5 in * 480 dpi = 240x240 px |
xxxhdpi | 640 dpi | 0.5 x 0.5 in | 0.5 in * 640 dpi = 320x320 px |
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()));
Dimension, Only changing the width/height
I ended up doing it the way Kleopatra said. Not changing the preferredSize but letting the layout manager do the job. Since this is the proper way to change the size of a component.
how to use dip and sip in android
Check this out.
Density-independent pixel (dip)
A virtual pixel unit that applications can use in defining their UI, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, the baseline density assumed by the platform (as described later in this document). At run time, the platform transparently handles any scaling of the dip units needed, based on the actual density of the screen in use. The conversion of dip units to screen pixels is simple: pixels = dips * (density / 160). For example, on 240 dpi screen, 1 dip would equal 1.5 physical pixels. Using dip units to define your application's UI is highly recommended, as a way of ensuring proper display of your UI on different screens.
Related Topics
Send Touch Events to a Device via Adb
Extending Relativelayout, and Overriding Dispatchdraw() to Create a Zoomable Viewgroup
What Is the Correct Way to Specify Dimensions in Dip from Java Code
How to Rotate a Bitmap 90 Degrees
How to Call Rest API from an Android App
Bluetooth Pairing Without User Confirmation
Accessing Google Account Id /Username via Android
Monitor Network Activity in Android Phones
Googleplayservicesutil VS Googleapiavailability
Why Use Armeabi-V7A Code Over Armeabi Code
Surfaceview Flashes Black on Load
Singleton with Parameter in Kotlin
Bulk Insertion on Android Device
Android Show Notification with a Popup on Top of Any Application