tags:

views:

9930

answers:

3

Hi,

I created some custom elements and I want to programatically place them to the upper right corner (n pixels from the top edge and m pixels from the right edge) therefore I need to get the screen width and screen height and then set position:

int px = screenWidth - m; int py = screenWidth - n;

Does anyone know how to get screenWidth and screenHeight in the main Activity?

Thanks

+2  A: 

First get view (eg. by findViewById()) and then you can use getWidth() on the view itself.

Marcin Gil
+24  A: 

If you want the the display dimensions in pixels you can use

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

For the use case you're describing however a margin/padding in the layout seems more appropriate.

Josef