Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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.

— asked March 11th 2010 by Jacob Williams
  • mobile
  • screen size
0 Comments

4 Answers

  • Accepted Answer

    try

    Titanium.Platform.displayCaps.platformWidth,

    Titanium.Platform.displayCaps.platformHeight,

    — answered March 11th 2010 by Nolan Wright
    permalink
    0 Comments
  • 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.

    — answered July 13th 2011 by Mark Pemburn
    permalink
    1 Comment
    • For designing based on absolute position, i need also to know the height of the status bar. The question is answered here: http://developer.appcelerator.com/question/122481/height-of-status-bar

      — commented July 14th 2011 by Admin i2Factory
  • Its in:
    Titanium.Platform.displayCaps.platformWidth
    and
    Titanium.Platform.displayCaps.platformHeight

    Its always a goof idea to look for something in the KitchenSink Example-App if you dont find it in the documentation.

    — answered March 11th 2010 by Jan Renz
    permalink
    0 Comments
  • 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?

    — answered July 13th 2011 by Admin i2Factory
    permalink
    1 Comment
    • Sorry, my problem is to find out the size of the Status Bar

      — commented July 13th 2011 by Admin i2Factory
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.