Question Detail

Get Screen width and height

1 years ago Views 198 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 - 
      game.findViewById(R.id.flag).getHeight());
}


Thread Reply

Anonymous

- 1 years ago

Using this code you can get 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);
 

Anonymous

- 1 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