Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell screen resolution in "DP"?

Tags:

android

I am working on an application based on Galaxy S at the moment. I know that Galaxy S is 480 px wide and 800 px tall, but how much is that in DP?

Lets say if I want to have two Layout side by side, I'll have them setting to 240 px. But how do I know what value I should use in DP unit?

like image 781
RobGThai Avatar asked Aug 20 '10 11:08

RobGThai


People also ask

What is dp in screen resolution?

A dp is equal to one physical pixel on a screen with a density of 160. To calculate dp: dp = (width in pixels *... A dp is equal to one physical pixel on a screen with a density of 160.

How many pixels is a dp?

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.

Is dp the same as pixels?

To preserve the visible size of your UI on screens with different densities, you must design your UI using density-independent pixels (dp) as your unit of measurement. One dp is a virtual pixel unit that's roughly equal to one pixel on a medium-density screen (160dpi; the "baseline" density).


1 Answers

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.

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

[edit] I just had to used this. Using the DisplayMetrics.density returns only 0.75, 1 and 1.5. Use DisplayMetrics.densityDpi instead or change the math to pixels = dips * DisplayMetrics.density

like image 189
WarrenFaith Avatar answered Sep 30 '22 16:09

WarrenFaith