How to get screen size in pixels in Android

Posted by dmcentire on Fri, 06 Dec 2019 01:37:48 +0100

I've created some custom elements that I want to programmatically place in the top right corner (top edge n pixels, right edge m pixels). Therefore, I need to get the screen width and height, and then set the position:

int px = screenWidth - m;
int py = screenHeight - n;

How do I get screen width and screen height in the main activity?

#1 building

First get the view (for example, through findViewById()), and then use it on the view itself getWidth() .

#2 building

If you want to display dimensions in pixels, you can use the getSize :

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

If you are not in Activity, you can get the default Display through window service:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

If you are in a fragment and want to do so, use Activity.WindowManager (in Xamarin.Android) or getActivity(). getWindowManager () (in Java).

Before introducing getSize (in API level 13), you can use the now deprecated getWidth and getHeight methods:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

However, for the use case you are describing, margins / padding in the layout seems more appropriate.

Another way is to: DisplayMetrics

Describes the structure of general information about the display, such as its size, density, and font scaling. To access the displaymetric member, initialize an object like this:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

We can use widthPixels to get the following information:

The absolute width of the display, in pixels

Example:

Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels);

#3 building

Find the width and height of the screen:

width = getWindowManager().getDefaultDisplay().getWidth();
height = getWindowManager().getDefaultDisplay().getHeight();

With this tool, we can get the latest SDK 13 and later.

// New width and height
int version = android.os.Build.VERSION.SDK_INT;
Log.i("", " name == "+ version);
Display display = getWindowManager().getDefaultDisplay();
int width;
if (version >= 13) {
    Point size = new Point();
    display.getSize(size);
    width = size.x;
    Log.i("width", "if =>" +width);
}
else {
    width = display.getWidth();
    Log.i("width", "else =>" +width);
}

#4 building

public class AndroidScreenActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        String str_ScreenSize = "The Android Screen is: "
                                   + dm.widthPixels
                                   + " x "
                                   + dm.heightPixels;

        TextView mScreenSize = (TextView) findViewById(R.id.strScreenSize);
        mScreenSize.setText(str_ScreenSize);
    }
}

#5 building

(the 2012 answer may be out of date) if you want to support versions prior to Honeycomb, you need to achieve backward compatibility before API 13. Similar:

int measuredWidth = 0;
int measuredHeight = 0;
WindowManager w = getWindowManager();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    Point size = new Point();
    w.getDefaultDisplay().getSize(size);
    measuredWidth = size.x;
    measuredHeight = size.y;
} else {
    Display d = w.getDefaultDisplay();
    measuredWidth = d.getWidth();
    measuredHeight = d.getHeight();
}

Of course, deprecated methods will eventually be removed from the latest SDK, but although we still rely on most users with Android 2.1, 2.2 and 2.3, this is the rest of us.

Topics: Android SDK Fragment Java