Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,810,588 Members 45,287 Now Online
XDA Developers Android and Mobile Development Forum

[Q] How to get screen dimensions

Tip us?
 
emerals stream
Old
(Last edited by emerals stream; 16th January 2014 at 07:18 PM.)
#1  
emerals stream's Avatar
Junior Member - OP
Thanks Meter 3
Posts: 20
Join Date: Nov 2013
Post [Q] How to get screen dimensions

I created some custom elements and I want to programmatically 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?
 
darvark
Old
#2  
Junior Member
Thanks Meter 0
Posts: 8
Join Date: Jan 2014
Location: Wroclaw
Quote:
Originally Posted by emerals stream View Post
I created some custom elements and I want to programmatically 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?
Try google it
http://stackoverflow.com/questions/1...een-dimensions
 
MattMatt0240
Old
#3  
MattMatt0240's Avatar
Senior Member
Thanks Meter 99
Posts: 225
Join Date: Sep 2011
Try something like this

Code:
 Point size = new Point();
WindowManager wManager = getWindowManager();

w.getDefaultDisplay().getSize(size);

(int)screenWidth = size.x;
(int)screenHeight = size.y;
Noted to self thrice via tapatalk
Root it and boot it!

Samsung Galaxy Note 3 | PAC-ROM

Retired phones: Epic Touch 4G| Evo shift 4g | Evo 4g
 
GRYMALA
Old
#4  
Junior Member
Thanks Meter 6
Posts: 19
Join Date: May 2014
...or to use DisplayMetrics

Code:
DisplayMetrics dm = this.getContext().getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels
The Following User Says Thank You to GRYMALA For This Useful Post: [ Click to Expand ]
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes