Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

Post [Q] How to get screen dimensions

OP emerals stream

16th January 2014, 05:56 PM   |  #1  
emerals stream's Avatar
OP Junior Member
Thanks Meter: 3
 
20 posts
Join Date:Joined: Nov 2013
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?
Last edited by emerals stream; 16th January 2014 at 08:18 PM.
16th January 2014, 06:30 PM   |  #2  
Junior Member
Flag Wroclaw
Thanks Meter: 0
 
8 posts
Join Date:Joined: Jan 2014
More
Quote:
Originally Posted by emerals stream

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
30th January 2014, 08:35 AM   |  #3  
MattMatt0240's Avatar
Senior Member
Thanks Meter: 111
 
278 posts
Join Date:Joined: Sep 2011
Donate to Me
More
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
6th May 2014, 01:30 PM   |  #4  
Junior Member
Thanks Meter: 6
 
19 posts
Join Date:Joined: 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: [ View ]
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes