---
title: "How to get screen dimensions in pixels"  
description: "How to get screen dimensions in pixels"  
author: "Samuel Fernandes"  
published: 2015-04-23  
updated: 2015-04-23  
canonical: https://www.mindstick.com/forum/23123/how-to-get-screen-dimensions-in-pixels  
category: "android"  
tags: ["android", "android layout"]  
reading_time: 1 minute  

---

# How to get screen dimensions in pixels

I created some [custom](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements), and I want to programmatically place them to the upper [right](https://www.mindstick.com/articles/12766/check-whether-you-are-doing-digital-transformation-right-or-not) corner (n pixels from the top [edge](https://yourviews.mindstick.com/view/85544/beyond-locks-and-passcodes-the-cutting-edge-of-mobile-data-security) and m pixels from the right edge). Therefore I need to get the [screen width](https://www.mindstick.com/forum/158674/how-can-you-make-an-image-adjust-its-size-based-on-the-screen-width) and screen height and then set position:\
[int](https://www.mindstick.com/forum/159137/how-to-convert-date-int-to-date-in-sql) px = screenWidth - m;int py = screenHeight - n;How do I get screenWidth and screenHeight in the main [Activity](https://www.mindstick.com/forum/12894/how-to-force-recreation-of-activity-fragment-on-restore)?

## Replies

### Reply by Anonymous User

if you want the display dimensions in pixels you can use getSize:\

```
Display display = getWindowManager().getDefaultDisplay();Point size = new Point();display.getSize(size);int width = size.x;int height = size.y;
```

If you're not in an Activity you can get the default Display via WINDOW_SERVICE:\

```
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Display display = wm.getDefaultDisplay();
```

Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:\

```
Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth();  // deprecatedint height = display.getHeight();  // deprecated
```

For the use case you're describing however, a margin/padding in the layout seems more appropriate.


---

Original Source: https://www.mindstick.com/forum/23123/how-to-get-screen-dimensions-in-pixels

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
