How to get the width and height of the screen

I tried using the following code to get the width and height of the screen in Android app development:

Display display = getWindowManager().getDefaultDisplay(); int screenWidth = display.getWidth(); int screenHeight = display.getHeight(); 

but I got a NullPointerException for my display , why? How to get the width and height of the screen?

+50
android android-layout android-activity android-emulator android-widget
Jun 29 2018-11-12T00:
source share
13 answers

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); 
+121
Jun 29 2018-11-11T00:
source share

As part of the action, you can call:

 int width = this.getResources().getDisplayMetrics().widthPixels; int height = this.getResources().getDisplayMetrics().heightPixels; 

When you are in a view, you need to get the context first:

 int width = getApplicationContext().getResources().getDisplayMetrics().widthPixels; int height = getApplicationContext().getResources().getDisplayMetrics().heightPixels; 

This will work on all versions of Android available with API 1, and will never expire.

+33
Oct 17 '13 at 0:52
source share

From service:

 Display display= ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); 
+19
Jun 29 '11 at 12:41
source share

Here is what finally helped me:

 DisplayMetrics metrics = this.getResources().getDisplayMetrics(); int width = metrics.widthPixels; int height = metrics.heightPixels; 
+16
Jun 09 '12 at 2:10
source share

Try using context.getResources().getDisplayMetrics() if you have context.

+11
Jun 29 '11 at 12:37
source share
 DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int height = metrics.heightPixels; int width = metrics.widthPixels; 

I think the code you wrote is out of date.

+8
May 7 '12 at 5:43
source share
 if (Build.VERSION.SDK_INT >= 11) { Point size = new Point(); try { this.getWindowManager().getDefaultDisplay().getRealSize(size); screenWidth = size.x; screenHeight = size.y; } catch (NoSuchMethodError e) { screenHeight = this.getWindowManager().getDefaultDisplay().getHeight(); screenWidth=this.getWindowManager().getDefaultDisplay().getWidth(); } } else { DisplayMetrics metrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(metrics); screenWidth = metrics.widthPixels; screenHeight = metrics.heightPixels; } 
+4
Sep 07 '13 at 10:38 on
source share

Try using the following code to get the width and height of the screen.

 int widthOfscreen =0; int heightOfScreen = 0; DisplayMetrics dm = new DisplayMetrics(); try { ((Activity) context).getWindowManager().getDefaultDisplay() .getMetrics(dm); } catch (Exception ex) { } widthOfscreen = dm.widthPixels; heightOfScreen = dm.heightPixels; 
+3
Jun 29 '11 at 12:39 on
source share
 WindowManager w = getWindowManager(); Display d = w.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); d.getMetrics(metrics); Log.d("WIDTH: ", String.valueOf(d.getWidth())); Log.d("HEIGHT: ", String.valueOf(d.getHeight())); 
+3
Jun 29 2018-11-11T00:
source share
  /* *DisplayMetrics: A structure describing general information about a display, such as its size, density, and font scaling. * */ DisplayMetrics metrics = getResources().getDisplayMetrics(); int DeviceTotalWidth = metrics.widthPixels; int DeviceTotalHeight = metrics.heightPixels; 
+2
Aug 20 '15 at 9:02
source share
  int scrWidth = getWindowManager().getDefaultDisplay().getWidth(); int scrHeight = getWindowManager().getDefaultDisplay().getHeight(); 
+1
Sep 29 '13 at 18:49
source share
 Display display = getActivity().getWindowManager().getDefaultDisplay(); int screenWidth = display.getWidth(); int screenHeight = display.getHeight(); Log.d("Tag", "Getting Width >> " + screenWidth); Log.d("Tag", "Getting Height >> " + screenHeight); 

This worked correctly in my application

0
Jun 19 '13 at 11:13
source share

Alternative way to use the function

 private int[] getScreenSIze(){ DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int h = displaymetrics.heightPixels; int w = displaymetrics.widthPixels; int[] size={w,h}; return size; } 

when you click or in your create function add the following code

 int[] screenSize= getScreenSIze(); int width=screenSize[0]; int height=screenSize[1]; screenSizes.setText("Phone Screen sizes \n\n width = "+width+" \n Height = "+height); 

See demo source code

0
Jan 20 '17 at 7:49
source share



All Articles