How to get screen width in dpi? - android

How can I get the screen width dpi?
DisplayMetrics metrics = getResources().getDisplayMetrics();
int f = metrics.densityDpi;
// getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int dp = (int)(width / (metrics.density));
I do this, so lets assume my densitydpi is 120..and widthpixel 240..
via calculation i get 240/0.75.... so I have 320 widthdpi?
However that's not the case....widthdpi is smaller then 320 I think...because 320 goes with me wrong in the screen.... using 120 works.
on Nexus 4...with width 768 and densitydpi 320...I divide 768/2 and i get 384 correct width density dpi (it works)
But on other ones like 240, 160, 120 density dpi..the calculation of width dpi seems wrong ...

Per the Supporting Multiple Screens guide's definition of DP
px = dp * (dpi / 160)
Therefore
dp = px / (dpi / 160)
or equivalently
dp = px * 160 / dpi
Remember that dp stands for 'density-independent pixel' - i.e., 1dp is the same physical size on a ldpi device as it is on an xxhdpi device. Therefore you should expect all phones to have roughly ~300-400dp of width, noting the bucket sizes by dp:
xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp

Related

What are the screen sizes of my Android emulators in dp?

I have the following emulators on my Windows machine:
what are their sizes in dp?
For example, when I start my app on selected Nexus emulator, its demens are loaded from
values-sw720dp
but not from
values-sw1200dp
or
values-sw1600dp
So the question is, for example, what exact size of this emulator in dp?
Convert dp units to pixel units:
px = dp * (dpi / 160) /* density */
Density qualifiers mapping:
ldpi (~120dpi)
mdpi (~160dpi)
hdpi (~240dpi)
xhdpi (~320dpi)
xxhdpi (~480dpi)
xxxhdpi (~640dpi)
With a -sw[N]dp (smallestWidth) modifier you are saying:
Use this resource on any device whose smallest dimension (screen's smallest dimension) is [N]dp or greater.
So, If you selected emulator with screen size 2560px x 1600px (xhdpi ~320dpi) its smallestWidth will be:
dp = px / (dpi / 160)
dp = 1600 / (320 / 160)
dp = 800
sw720dp < 800dp < sw1200dp < sw1600dp
References: AlternativeResources, ScreenDensities, DisplayMetrics

Does every Android screen have the same dpi?

I seem to be very confused after reading the Android Developer Supporting Multiple Screens. I thought the dpi was the amount of pixels on each respected screen in order to make the rendered object appear the same on every screen. So, in a way this means dpi is basically a percentage of the screen? For example, if I move an object 10dp in a south bound direction is this basically moving the object a percent of the screen south?
I also thought the dp was a scaled version of pixels to a medium size screen. So does this mean every android device has the same dp screen size?
Some background info:I am trying to develop a game. In this game objects are falling down the screen. So I would like the objects to appear to be falling and moving around the screen at relatively the same speed on every Android device. Any tips or info to help me get on track or am I way off in my approach?
The current methods I am using for conversions:
public static int dpToPx(Context context, float dp) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}
public static int pxToDp(Context context, float px) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int dp = Math.round(px / (displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
return dp;
}
what i would do is get the width and height of the device and than use fractions of that, then it will be the same on all devices
here i got this off of this site maybe it will come in handy for you
xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
Generalised Dpi values for screens:
ldpi Resources for low-density (ldpi) screens (~120dpi)
mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240dpi).
xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).
Therefore generalised size of your resources (assuming they are full screen):
ldpi
Vertical = 426 * 120 / 160 = 319.5px
Horizontal = 320 * 120 / 160 = 240px
mdpi
Vertical = 470 * 160 / 160 = 470px
Horizontal = 320 * 160 / 160 = 320px
hdpi
Vertical = 640 * 240 / 160 = 960px
Horizontal = 480 * 240 / 160 = 720px
xhdpi
Vertical = 960 * 320 / 160 = 1920px
Horizontal = 720 * 320 / 160 = 1440px
px = dp*dpi/160
for full information please visit
http://www.captechconsulting.com/blog/steven-byle/understanding-density-independence-android

Best Screen Resolution to test Android Game

I am building a game for Android. I want to test it for as many phone and tablet resolutions as possible. Can somebody tell me what are the main resolutions i need to test so that i can be assured of it running fine on all Android supported phones and tables.
I am looking for something like 1024x768... etc. This is because I can set the resolution in unity and test it on my screen.
i got this off of here for android maybe this will help
xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
Generalised Dpi values for screens:
ldpi Resources for low-density (ldpi) screens (~120dpi)
mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240dpi).
xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).
Therefore generalised size of your resources (assuming they are full screen):
ldpi
Vertical = 426 * 120 / 160 = 319.5px
Horizontal = 320 * 120 / 160 = 240px
mdpi
Vertical = 470 * 160 / 160 = 470px
Horizontal = 320 * 160 / 160 = 320px
hdpi
Vertical = 640 * 240 / 160 = 960px
Horizontal = 480 * 240 / 160 = 720px
xhdpi
Vertical = 960 * 320 / 160 = 1920px
Horizontal = 720 * 320 / 160 = 1440px
px = dp*dpi/160
If you want to test it inside the Editor it is better that you set an aspect ratio and rezise the window to your needs. This gives you a flexible tester instead of making static resolutions.

Difficulty to understand how to support multiple screen

I have seen so many questions on StackOverFlow how to support multiple screens. But most of the answers provide this link and this. And in the first one link I have got this. I am working with screen sizes first time so please help me.
xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
And in case of xhdpi they suggest 320 density. I am confused so I have following questions.
Like if I want to create Background image for xlarge screen then 960dp x 720dp means I have to create image with width 720 pixels and height 960 pixels and resolution 320 in portrait mode?
And if I want to create Background image for large screen then 640dp x 480dpp means I have to create image with width 480 pixels and height 640 pixels and resolution more than 200 in portrait mode?
The sizes above is for landscape mode i.e 960dp for width and 720dp for height ?
From Android: Supporting Multiple Screens
px = dp * (dpi / 160)
So for your example:
Your image dimensions are 960dp x 720dp and your dpi is 320 (which you can see falls under XHDPI).
Height = 960dp * (320 / 160) = 1920px
Width = 720dp * (320 / 160) = 1440px
Height and width are arbitrary depending on the orientation of the phone
Refer this for Conversion of dp into px for each dpi (Android)
In android-
px = dp * (dpi / 160)
So in your first question 960dp x 720dp at 320dpi means 1920px x 1440px for xlarge screen in landscape mode using above formula.
640dp x 480dp at 240dpi means 960px x 720px for large screen in landscape mode.
To learn more about check this.
dp means density independent pixel.
When you say for example 300dp means 300 pixels on a medium density (160dpi) screen.
So if you want to translate dp on real resolution you have to multiply the dp for the density scale factor:
es
300dp are
300 px on a mdpi screen
450 px on a hdpi screen
600 px on a xhdpi screen
so if you specify resources you should remember to use both size and density modifiers
a background for a 480x800 px medium screen phone woul be picked from normal-hdpi folder an so on

background image size for mobile devices

I want to show a background image in my andriod application.
What is the ideal image size (width and height) to fit in all screen resolutions for mobile?
id say ideal would be to use the highest screen density so the app will scale it down for lower screen resolutions that way you wont loose quality on bigger screens, heres the chart i got off of here
xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
Generalised Dpi values for screens:
ldpi Resources for low-density (ldpi) screens (~120dpi)
mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240dpi).
xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).
Therefore generalised size of your resources (assuming they are full screen):
ldpi
Vertical = 426 * 120 / 160 = 319.5px
Horizontal = 320 * 120 / 160 = 240px
mdpi
Vertical = 470 * 160 / 160 = 470px
Horizontal = 320 * 160 / 160 = 320px
hdpi
Vertical = 640 * 240 / 160 = 960px
Horizontal = 480 * 240 / 160 = 720px
xhdpi
Vertical = 960 * 320 / 160 = 1920px
Horizontal = 720 * 320 / 160 = 1440px
px = dp*dpi/160
please refer following links..
Support multiple screens
Best practise
testing for multiple screens
This along with #JRowan answer will guide you in proper direction..

Categories

Resources