I'm using this:
int wi=getWindowManager().getDefaultDisplay().getWidth();
to get the screen size in pixels. Is there a way to get the screen size in dpi
???
I'm doing this to select different layouts base on the screen size.
I'm using this:
int wi=getWindowManager().getDefaultDisplay().getWidth();
to get the screen size in pixels. Is there a way to get the screen size in dpi
???
I'm doing this to select different layouts base on the screen size.
Here is a way to calculate the width and height of the screen in pixels.
you must create metric object and do below
and every you want to use this method you can set context with setcontext method and call best method with your purpose like this code this code is oncreateView of main activity:
DisplayMetrics
can tell you the screen size in pixels, plus the screen density. From there, you can calculate the screen size indp
.This is automatically handled for you by the resource framework, if you put your layouts in the proper directories (e.g.,
res/layout-large/
,res/layout-sw600dp/
).The "screen size in dpi" is a meaningless statement. Do you just want to get the DPI of the display?
Secondly, don't do this.
Seriously, stop. Don't do it.
Use the layout folders as they are intended. If you need a different layout for HDPI, put your custom layout in
layout-hdpi
.That said, if you just need the density: