17

I have a Phonegap app designed to run on Android phones and tablets. The scale of text and images looks fine on a phone, but too small on a 7” tablet.

Is there a way to set the scale for different screen sizes/densities that works for a Phonegap-based app? For a native Android app, multiple screen layouts (as mentioned in the android docs) would be a solution, but can this be used for a Phonegap-based app?

I could use CSS media queries to set font sizes based on screen size, but I want to scale the entire interface (including images) proportionally.

I tried using the CSS zoom property along with media queries to target specific screen sizes, but this screws up absolute positioning and interferes with the function of UI elements such as iScroll:

@media only screen and (min-device-width : 768px) {
    body{
        zoom: 125%;
    }   
}
@media only screen and (min-device-width : 1024px){ 
    body{
        zoom: 150%;
    }
}

I also tried using the targetdensity-dpi meta viewport property - adjusting it to 120dpi makes the scale better on the 7” tablet, but too large on the phone. Since this is hard-coded in HTML rather than CSS, media queries can’t be used to vary it based on screen size:

<meta name="viewport" 
   content="width=device-width, initial-scale=1, targetdensity-dpi=120dpi">

Here's some screenshots from a testcase Phonegap app:

DaveAlden
  • 30,083
  • 11
  • 93
  • 155
  • 4
    The app (in fact it's a [series of apps](https://play.google.com/store/apps/developer?id=Working+Edge+Ltd)) is written, tested and functioning perfectly well on android phones, I just want to expand it to 7" GPS-enabled tablets. It also works fine on iPhone/iOS (with appropriate native plugins), proving Phonegap's worth in that I don't have to write and maintain two completely separate versions in Java and Objective-C. – DaveAlden Oct 18 '13 at 18:54
  • Until you get to tablets ... – Howard Pautz Oct 18 '13 at 18:56
  • 4
    Hence my question and faith in the wonderful folks on stackoverflow... :-) – DaveAlden Oct 18 '13 at 19:14
  • 1
    @HowardPautz [Something serious](http://phonegap.com/app/). Dpa99c, have you looked into any responsive frameworks like [Bootstrap](http://getbootstrap.com/) or [Foundation](http://foundation.zurb.com/)? – Andrew Lively Oct 18 '13 at 19:41
  • @AndrewLively - like I said, I love phonegap because people see things like those and think it will be better than mobile-independent sliced bread. ... until they chew on it a while... – Howard Pautz Oct 18 '13 at 19:44
  • @AndrewLively - don't these responsive frameworks just use CSS media queries under the hood? I have my JQM user interface all laid out, so I was just wondering if anyone had a neat trick I could employ to apply an all-in-one scale factor. Otherwise I can achieve a solution with a bit of laborious CSS3 with media queries to pick out the different screen sizes. – DaveAlden Oct 18 '13 at 20:09
  • Awesome, thanks. Good question now is how to deal with the higher-dpi units... Media queries seems a little ungraceful..... – Jamie Hutber Mar 05 '14 at 22:56
  • Check this: http://v1.jontangerine.com/log/2007/09/the-incredible-em-and-elastic-layouts-with-css That's what I've been doing, measure everithing in em, and use media queries to set the font-size of the body. – Guillermo Gutiérrez Apr 08 '14 at 20:44

4 Answers4

22

Before we give up on phonegap lets try improve it. I was stuck until I solved it using this solution.

Change your viewport to this :

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, target-densitydpi=medium-dpi, user-scalable=0" />

Ref : Phonegap Application text and layout too small

Community
  • 1
  • 1
Kennedy Nyaga
  • 3,455
  • 1
  • 26
  • 25
  • 1
    Yes, the "target-densitydpi=medium-dpi" is the key I needed thanks, although I'm using "target-densitydpi=high-dpi" as it suits my css better. – toddles_fp Jan 10 '14 at 02:24
4

Just noticed I never answered this. The solution I used in the end was to use media queries to explicitly set font sizes and image assets based on the device size. Device testing showed this worked on all my target devices: iPhone, iPad, Android phones, Android 7" tablets, Android 10" tables. Hope it helps someone else.

For example:

/* Start with default styles for phone-sized devices */
    p,li{
        font-size: 0.9em;
    }
    h1{
        font-size: 1.2em;
    }
    button{
        width: 24px;
        height: 12px;
    }
    button.nav{
        background-image: url('img/phone/nav.png');
    }

@media only screen and (min-device-width : 768px) { /* 7" tablets */
    p, li{
        font-size: 1.3em !important;
    }
    h1{
        font-size: 1.6em !important;
    }
    button{
          width: 32px;
          height: 16px;
    }
    button.nav{
        background-image: url('img/tablet7/nav.png');
    }
}
@media only screen and (min-device-width : 1024px) { /* 10" tablets and iPad */
    p, li{
        font-size: 1.5em !important;
    }
    h1{
        font-size: 1.8em !important;
    }
    button{
        width: 48px;
        height: 24px;
    }
    button.nav{
        background-image: url('img/tablet10/nav.png');
    }
}
DaveAlden
  • 30,083
  • 11
  • 93
  • 155
  • Hi, How about on a phone in a landscape mode ? any advice ? Thanks ! – user636525 Nov 02 '14 at 02:22
  • You could use the window.onorientationchange event to add/remove a CSS class to the body. [See here](http://stackoverflow.com/questions/5284878/how-do-i-correctly-detect-orientation-change-using-javascript-and-phonegap-in-io) for details – DaveAlden Nov 03 '14 at 11:14
2

Consider that target-densitydpi has been deprecated and removed from Webkit according to Pete LePage (https://plus.google.com/+GoogleChromeDevelopers/posts/BKHdSgQVFBB), I think a better approach is to use media queries to target high-dpi devices before dropping phonegap

JohnFalcon
  • 71
  • 6
  • 1
    Depends on the browser, yes. Android 2.3 respects it, while Android 4.4 totally ignores it and magically adjusts the zoom to the proper level according to device dpi, without any additional attributes at all. – andreszs Jan 07 '15 at 23:50
1

I have a couple of different solutions to suggest:

  1. Change viewport dynamically using Javascript (more info here)
  2. Set all sizes in rem units rather than px. Then you can scale everything by adjusting document font-size. Sketching an example:

    function resize() {
    var w = document.documentElement.clientWidth;
    var h = document.documentElement.clientHeight;
    var styleSheet = document.styleSheets[0];
    // ar = aspect ratio h/w; Replace this with your apps aspect ratio
    var ar = 1.17;
    // x = scaling factor
    var x = 0.1; 
    var rem;
    if (h / w > ar) { // higher than aspect ratio
        rem = x * w;
    } else { // wider than aspect ratio
        rem = x * h;
    }
    document.documentElement.style.fontSize = rem + 'px';
    }
    
Andrei Cristian Prodan
  • 1,114
  • 4
  • 17
  • 34
Per Quested Aronsson
  • 11,380
  • 8
  • 54
  • 76