Get Screen Width and Height in Android

Get screen width and height in Android

Using this code, you can get the runtime display's width & height:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

In a view you need to do something like this:

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

In some scenarios, where devices have a navigation bar, you have to check at runtime:

public boolean showNavigationBar(Resources resources)
{
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
return id > 0 && resources.getBoolean(id);
}

If the device has a navigation bar, then count its height:

private int getNavigationBarHeight() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableHeight = metrics.heightPixels;
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.heightPixels;
if (realHeight > usableHeight)
return realHeight - usableHeight;
else
return 0;
}
return 0;
}

So the final height of the device is:

int height = displayMetrics.heightPixels + getNavigationBarHeight();

How to get screen dimensions as pixels in Android

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

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

If you're not in an Activity you can get the default Display via WINDOW_SERVICE:

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

If you are in a fragment and want to acomplish this just use Activity.WindowManager (in Xamarin.Android) or getActivity().getWindowManager() (in java).

Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:

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

For the use case, you're describing, however, a margin/padding in the layout seems more appropriate.

Another way is: DisplayMetrics

A structure describing general information about a display, such as its size, density, and font scaling. To access the DisplayMetrics members, initialize an object like this:

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

We can use widthPixels to get information for:

"The absolute width of the display in pixels."

Example:

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

API level 30 update

final WindowMetrics metrics = windowManager.getCurrentWindowMetrics();
// Gets all excluding insets
final WindowInsets windowInsets = metrics.getWindowInsets();
Insets insets = windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars()
| WindowInsets.Type.displayCutout());

int insetsWidth = insets.right + insets.left;
int insetsHeight = insets.top + insets.bottom;

// Legacy size that Display#getSize reports
final Rect bounds = metrics.getBounds();
final Size legacySize = new Size(bounds.width() - insetsWidth,
bounds.height() - insetsHeight);

How to get correct screen width (px) in Android Studio?

Hi I m not sure about this but try changing the resolution on your phone.
https://www.technobezz.com/how-to-change-screen-resolution-on-samsung-galaxy-s20-ultra/

Getting the actual screen height (android)

see this answer

if your API level > 13 try this if you are in activity

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

If you're not in an Activity you can get the default Display via WINDOW_SERVICE:

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

getWidth and getHeight is for API level 13 or less

UPDATE

For API 17 and higher method Display.getRealSize() returns full size of screen, as mentioned in documentation:

Gets the real size of the display
without subtracting any window decor or applying any compatibility
scale factors.

The size is adjusted based on the current rotation of the display.

The real size may be smaller than the physical size of the screen when
the window manager is emulating a smaller display (using adb shell am
display-size).

I want to be able to get the screen height and width in Android

You are calling getResources() before the super.onCreate(Bundle) is called. You must call getResources() after onCreate(). Move your code to onCreate or onStart:

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics display = getResources().getDisplayMetrics();
int width = display.widthPixels;
int height = display.heightPixels;
}

Get reported screen width and height

Interesting question. It gives me adjusted dimensions in pixels. I have tested it now in my Pixel running Android O.

Method 1:

        DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

Method 2:

        Configuration configuration = getResources().getConfiguration();
int screenWidthDp = configuration.screenWidthDp;
int screenHeightDp = configuration.screenHeightDp;
int smallestScreenWidthDp = configuration.smallestScreenWidthDp;

Both methods returned the adjusted dimensions. The first method returns the device screen dimensions. The second method returns the dimensions of the app screen. These two result would vary when there are two apps open in multi-window mode.

Getting Screen width and height in Android

What you can do is:
you already have randomly generated x and y,
x + button.getWidth() to it and check if it is less than screen width if yes your x is well in bound if no regerenate your random x and y, similary if x is in bound do it for y and check y + button.getHeight() is less than screen height it will do.



Related Topics



Leave a reply



Submit