ChatGPT解决这个技术问题 Extra ChatGPT

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

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.


T
Top-Master

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;

Why it is necessary to call WindowManager? What about this code? DisplayMetrics displayMetrics = resources.getDisplayMetrics(); float screenWidthDp = displayMetrics.widthPixels / displayMetrics.density;
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;
@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.
s
serjlee

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.


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.
This is only for API Level 13 and up
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.
T
TheYogi

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 }

But where is the density. I guess there needs some more lines.
The density comes from DisplayMetrics within the run block.
a
amalBit

How about using this instead ?

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

M
Mykhailo Gaidai

You are missing default density value of 160.

    2 px = 3 dip if dpi == 80(ldpi), 320x240 screen
    1 px = 1 dip if dpi == 160(mdpi), 480x320 screen
    3 px = 2 dip if dpi == 240(hdpi), 840x480 screen

In other words, if you design you layout with width equal to 160dip in portrait mode, it will be half of the screen on all ldpi/mdpi/hdpi devices(except tablets, I think)


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?
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
S
Supun Madushanka
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;

Consider giving some explanation
B
Bolling

Answer in kotlin:

  context?.let {
        val displayMetrics = it.resources.displayMetrics
        val dpHeight = displayMetrics.heightPixels / displayMetrics.density
        val dpWidth = displayMetrics.widthPixels / displayMetrics.density
    }

A
Ali Azaz Alam

Get Screen Width and Height in terms of DP with some good decoration:

Step 1: Create interface

public interface ScreenInterface {

   float getWidth();

   float getHeight();

}

Step 2: Create implementer class

public class Screen implements ScreenInterface {
    private Activity activity;

    public Screen(Activity activity) {
        this.activity = activity;
    }

    private DisplayMetrics getScreenDimension(Activity activity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics;
    }

    private float getScreenDensity(Activity activity) {
        return activity.getResources().getDisplayMetrics().density;
    }

    @Override
    public float getWidth() {
        DisplayMetrics displayMetrics = getScreenDimension(activity);
        return displayMetrics.widthPixels / getScreenDensity(activity);
    }

    @Override
    public float getHeight() {
        DisplayMetrics displayMetrics = getScreenDimension(activity);
        return displayMetrics.heightPixels / getScreenDensity(activity);
    }
} 

Step 3: Get width and height in activity:

Screen screen = new Screen(this); // Setting Screen
screen.getWidth();
screen.getHeight();

Does this return height and width in pixel or dp?
as called .density that's why its give you in dp
I don't think it's a good idea to add tones of code to your app to solve a simple task
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.
S
Simon Featherstone

In the new world of Compose on one line

val (height, width) = LocalConfiguration.current.run { screenHeightDp.dp to screenWidthDp.dp }

S
Szabolcs Becze

This is a copy/pastable function to be used based on the previous responses.

  /**
     * @param context
     * @return the Screen height in DP
     */
    public static float getHeightDp(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
        return dpHeight;
    }

    /**
     * @param context
     * @return the screnn width in dp
     */
    public static float getWidthDp(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
        return dpWidth;
    }

m
math channel

If you just want to know about your screen width, you can just search for "smallest screen width" in your developer options. You can even edit it.


M
Muhammed naseef Koppilakkal

Try this:

Display display   = getWindowManager().getDefaultDisplay();
Point displaySize = new Point();
display.getSize(displaySize);
int width  = displaySize.x;
int height = displaySize.y;

e
eldarerathis

Your problem is with casting the float to an int, losing precision. You should also multiply with the factor and not divide.

Do this:

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

This answer is incorrect. From developer.android.com/guide/practices/…, Pixel = Dp * Density.