How to determine the screen width in terms of dp or dip at runtime in Android?

183,645

Solution 1

As @Tomáš Hubálek mentioned;
Try something like:

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();    
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;

OR

Try old answer:

Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
         
float density  = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth  = outMetrics.widthPixels / density;

Solution 2

I stumbled upon this question from Google, and later on I found an easy solution valid for API >= 13.

For future references:

Configuration configuration = yourActivity.getResources().getConfiguration();
int screenWidthDp = configuration.screenWidthDp; //The current width of the available screen space, in dp units, corresponding to screen width resource qualifier.
int smallestScreenWidthDp = configuration.smallestScreenWidthDp; //The smallest screen size an application will see in normal operation, corresponding to smallest screen width resource qualifier.

See Configuration class reference

Edit: As noted by Nick Baicoianu, this returns the usable width/height of the screen (which should be the interesting ones in most uses). If you need the actual display dimensions stick to the top answer.

Solution 3

2022 Answer simplified for Kotlin:

val widthDp = resources.displayMetrics.run { widthPixels / density }
val heightDp = resources.displayMetrics.run { heightPixels / density }

Or as one-liner:

val (height, width) = resources.displayMetrics.run { heightPixels/density to widthPixels/density }

Solution 4

How about using this instead ?

final DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
final float screenWidthInDp=displayMetrics.widthPixels/displayMetrics.density;
final float screenHeightInDp=displayMetrics.heightPixels/displayMetrics.density;

Solution 5

DisplayMetrics displayMetrics = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

int width_px = Resources.getSystem().getDisplayMetrics().widthPixels;

int height_px =Resources.getSystem().getDisplayMetrics().heightPixels;

int pixeldpi = Resources.getSystem().getDisplayMetrics().densityDpi;


int width_dp = (width_px/pixeldpi)*160;
int height_dp = (height_px/pixeldpi)*160;
Share:
183,645

Related videos on Youtube

Khushboo
Author by

Khushboo

Hi, I am a mobile application developer, developing the applications on Android and J2ME. I also have the expertise of J2EE, core concepts are: Servlets, JSP, RMI.

Updated on January 08, 2022

Comments

  • Khushboo
    Khushboo over 2 years

    I need to code the layout of the android widgets using dip/dp (in java files). At runtime if I code,

    int pixel=this.getWindowManager().getDefaultDisplay().getWidth();

    this return the screen width in pixels (px). To convert this to dp, I coded:

    int dp =pixel/(int)getResources().getDisplayMetrics().density ;

    This does not seem to be returning correct answer. I made the emulator of WVGA800 whose screen resolution is 480 by 800. When the run the emulator and let the code print the values of pixel and dp, it came to 320 in both. This emulator is 240 dpi whose scale factor would be 0.75.

  • Khushboo
    Khushboo about 13 years
    What you have answered is the logical reasoning behind my question. I understand this. How should I code this in java so that at runtime whatever the screen resolution is,correct dpi width is picked by the code?
  • Mykhailo Gaidai
    Mykhailo Gaidai about 13 years
    Hope I get it right this time :) Use DisplayMetrics outMetrics = null; getWindowManager().getDefaultDisplay().getMetrics(outMetrics‌​); within Activity. Then, outMetrics.density will give you the scale factor of the current device, as stated in the docs
  • Tomáš Hubálek
    Tomáš Hubálek over 11 years
    Why it is necessary to call WindowManager? What about this code? DisplayMetrics displayMetrics = resources.getDisplayMetrics(); float screenWidthDp = displayMetrics.widthPixels / displayMetrics.density;
  • Vance-Turner
    Vance-Turner over 11 years
    This answer is incorrect. From developer.android.com/guide/practices/…, Pixel = Dp * Density.
  • Nick Baicoianu
    Nick Baicoianu almost 10 years
    One important difference between using Configuration's screenWidthDp/screenHeightDp vs DisplayMetrics widthPixels/heightPixels is Configuration returns the usable display dimensions (minus the status bar etc), while DisplayMetrics returns the full screen dimensions.
  • marienke
    marienke over 9 years
    This didn't work for me using a very high density screen. It was as if it only got a small portion of the screen. This solution worked for me: stackoverflow.com/a/18712361/956975 Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y;
  • ono
    ono almost 9 years
    This is only for API Level 13 and up
  • François POYER
    François POYER almost 8 years
    @dsdsdsdsd : context is any instance of the Context class. Most notably, Activity is a sub class of Context (so use this in an Activity, and getActivity() in a Fragment). Application and Service are also sub classes of Context.
  • bigbounty
    bigbounty almost 7 years
    Consider giving some explanation
  • nmw
    nmw about 6 years
    To add to @NickBaicoianu's comment, both the getConfiguration() and getDisplayMetrics() methods work in multi-window mode. i.e. the sizes reflect that of the window, not the screen.
  • Taslim Oseni
    Taslim Oseni almost 5 years
    Does this return height and width in pixel or dp?
  • Ali Azaz Alam
    Ali Azaz Alam almost 5 years
    as called .density that's why its give you in dp
  • Ruslan Berozov
    Ruslan Berozov over 4 years
    I don't think it's a good idea to add tones of code to your app to solve a simple task
  • Ali Azaz Alam
    Ali Azaz Alam over 4 years
    Sir it's upto you whether to implement class or directly code it. From the defined class you can easily extract the code for direct implementation.
  • mcfly soft
    mcfly soft over 2 years
    But where is the density. I guess there needs some more lines.
  • Admin
    Admin about 2 years
    The density comes from DisplayMetrics within the run block.