How can I get screen width Android window size using Java as programming language for Android automation testing?

289    Asked by Amitraj in QA Testing , Asked on Apr 26, 2022

How to detect the screen size for different android phones?

I want the coordinate values like startx, starty, endx, endy. With that I can use the swipe method which takes these coordinates as parameters.

I used Dimension size = driver.manage().window().getSize();

Answered by Amit jaisawal

There are a few methods to get screen width Android,


getSize() methods
Display screen size = getWindowManager().getDefaultDisplay(); Point size = new Point(); screensize.getSize(size); int width = size.x; int height = size.y;
Ge default display size:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display screen size = wm.getDefaultDisplay();
getWidth() and getHeight() methods
Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); // deprecated int height = display.getHeight(); // deprecated
DisplayMetrics() method:
DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int height = displaymetrics.heightPixels; int width = displaymetrics.widthPixels;

Your Answer

Interviews

Parent Categories