Question Detail

Get Screen width and height

6 years ago Views 1386 Visit Post Reply

How I can get screen width and height for using this value in

@Override protected void onMeasure(int widthSpecId, int heightSpecId) {
   Log.e(TAG, "onMeasure" + widthSpecId);
   setMeasuredDimension(SCREEN_WIDTH, SCREEN_HEIGHT - 

Thread Reply


- 6 years ago

Using this code you can get runtime Display's Width & Height

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

In a view you need to do something like this:

((Activity) getContext()).getWindowManager()


- 6 years ago

There is a very simple answer and without pass context

public static int getScreenWidth() {
    return Resources.getSystem().getDisplayMetrics().widthPixels;

public static int getScreenHeight() {
    return Resources.getSystem().getDisplayMetrics().heightPixels;

and the result is the same as admin