Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling a Phonegap app for different Android screen sizes/densities?

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:

  • HTC HD2, target-density=default
  • HTC HD2, target-density=120dpi
  • Nexus 7, target-density=default
  • Nexus 7, target-density=120dpi
like image 658
DaveAlden Avatar asked Oct 18 '13 17:10

DaveAlden


People also ask

How do I deal with different screen sizes on Android?

The best way to create a responsive layout is to use ConstraintLayout as the base layout in your UI. ConstraintLayout enables you to specify the position and size of each view according to spatial relationships with other views in the layout. All the views can then move and resize together as the screen size changes.

How do I make my Android app fit the screen?

Open up Settings and then tap on Display. Scroll down and tap on Display size. In this new screen, drag the slider to left to shrink the display size or the right to enlarge it.


2 Answers

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

like image 158
Kennedy Nyaga Avatar answered Nov 09 '22 22:11

Kennedy Nyaga


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');
    }
}
like image 42
DaveAlden Avatar answered Nov 09 '22 20:11

DaveAlden