How do I get the mobile device's screen size?
Is it possible to get the screen size of the current mobile device? I looked for this functionality in the new documentation, but was unable to find anything.
4 Answers
-
Accepted Answer
try
Titanium.Platform.displayCaps.platformWidth,
Titanium.Platform.displayCaps.platformHeight,
-
One caveat: The .platformWidth and .platformHeight values are the width and height at that moment. If, for example you have an iPhone and the screen is being held in landscape orientation, Titanium.Platform.displayCaps.platformWidth will return "480". If you rotate to portrait and test it again, it'll give you "320". If, for some reason you wish to know the width and height as absolutes regardless of present orientation, you can use this method:
var pWidth = Ti.Platform.displayCaps.platformWidth; var pHeight = Ti.Platform.displayCaps.platformHeight; Ti.App.SCREEN_WIDTH = (pWidth > pHeight) ? pHeight : pWidth; Ti.App.SCREEN_HEIGHT = (pWidth > pHeight) ? pWidth : pHeight;
I put this code in my app.js so I can use Ti.App.SCREEN_WIDTH and Ti.App.SCREEN_HEIGHT throughout the app without need to test for these values again. Of course, if you need to do a calculation on the fly based on the present orientation, use the Ti.Platform.displayCaps values.
-
Its in:
Titanium.Platform.displayCaps.platformWidth
and
Titanium.Platform.displayCaps.platformHeightIts always a goof idea to look for something in the KitchenSink Example-App if you dont find it in the documentation.
-
Titanium.Platform.displayCaps.platformHeight get the height of the device.
Android and Iphone apps inserts automatically some header controls, that's why we cannot use platformHeight as reference to place views in relative positions.
In iphone, we can do something like:
var win = Ti.UI.createWindow({
fullscreen: true
});myapp.width.total = win.height;
Any idea how to get the real height of the view also for android devices?