If you call this outside the scope of the action, you need to pass the context (or get it through some other call). Then use this to get display metrics:
DisplayMetrics metrics = context.getResources().getDisplayMetrics(); int width = metrics.widthPixels; int height = metrics.heightPixels;
UPDATE: with API level 17+ you can use getRealSize :
Point displaySize = new Point(); activity.getWindowManager().getDefaultDisplay().getRealSize(displaySize);
If you want to get an available window size, you can use getDecorView to calculate the available area by subtracting the size of the decor view from the actual display size:
Point displaySize = new Point(); activity.getWindowManager().getDefaultDisplay().getRealSize(displaySize); Rect windowSize = new Rect(); ctivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(windowSize); int width = displaySize.x - Math.abs(windowSize.width()); int height = displaySize.y - Math.abs(windowSize.height()); return new Point(width, height);
getRealMetrics can also work (requires API level 17+), but I have not tried it yet:
DisplayMetrics metrics = new DisplayMetrics(); activity.GetWindowManager().getDefaultDisplay().getRealMetrics(metrics);
areyling Jun 29 2018-11-11T00: 00Z
source share