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 to manipulate webview to fit every android platform?

It seems that I am having difficulty fitting a url page, and customizing it for a QVGA. I've tried:

<meta content="minimum-scale=1.0, width=device-width, maximum-scale=1.0, user-scalable=no" name="viewport">

and tried:

var webview = Titanium.UI.createWebView({
    url:'index.htm',
    scalesPageToFit:true,
    Width:'auto',
    Height:'auto'
});

Are there any other tips that anyone could recommend?

Android API 2.1, QVGA and Titanium (1.2.2) SDK version 1.7.0.

Thank you.

— asked June 1st 2011 by David Craigster
  • android
  • coded
  • hard
  • height
  • meta
  • scalespagetofit
  • tag
  • url
  • webview
  • width
0 Comments

3 Answers

  • I had a heck of a time with that, too. Here are three things I did:

    1. Because I wanted a small bit of scalability:

      <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=2.0, user-scalable=yes" />
      
    2. scalesPageToFit: false for the webview itself. I'm not sure what you're after, but I want it to flow to the window size and not have, for instance, a horizontal scrollbar.

    3. In the webview's 'load' listener:

      view.evalJS("document.getElementById('body').style.fontSize = '" + viewFontSize + "pt'");
      

      I found that was necessary to force things to display at a sane and consistent font size (stored in viewFontSize).

    I'm sure you've tried every combination of 1 and 2. Hopefully 3 might help your situation a bit.

    — answered June 1st 2011 by K T
    permalink
    3 Comments
    • Hey KT. Thank you for the feed back. I actually tried 1 and 2, but 3 seems to be something I haven't come across. Unfortunately, they aren't what I'm looking for. The width of the url page isn't the problem, its the height. If only I could find a way to just squeeze the height into a QVGA.

      — commented June 1st 2011 by David Craigster
    • By the way KT, once I finish creating this app, do you think Google will give us the choice to select which phone to target? Thats if I cannot find a solution to this problem.

      — commented June 1st 2011 by David Craigster
    • This didn't work for me. Try setting device-width to an actual numeric figure. Doesn't matter what number. It seems that the 'device-width' parameter changes in the course of painting the webview, which gave me unreliable width (sometimes correct, sometimes 4x screen width)

      — commented September 17th 2012 by Wytze Schouten
  • Here is my work-around.

    Put the following line in your "load" event listener.

    webView.evalJS("var m=document.createElement(\"meta\");m.name = \"viewport\";m.content = \"width=320, user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1\";document.getElementsByTagName(\"head\")[0].appendChild(m);");
    
    — answered February 14th 2012 by Jack Lin
    permalink
    0 Comments
  • EvalJS didn't work for me (caused error), but setting width to 320 like you suggested instead of 'device-width' worked wonders. I'm starting to think that the webView's width changes a few times in the process of buidling and opening the webview, as I was getting inconsistent results, with faster devices experiencing more problems while slow devices usually got the width right. Adding big scripts like jQuery had the same effect: a slowly built DOM was more reliable than a quickly built DOM.

    — answered September 17th 2012 by Wytze Schouten
    permalink
    0 Comments
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.