17
votes

I have an HTML page and a full-screen Android webview. The HTML content is larger than the webview's visible area. I want to fit the HTML content to the width of webview.

What's really important is this: when the device's orientation changes, the webview will not reload, meaning it will just resize and the content would still fit to width.

One more thing: because the content has been scaled down, user can zoom it in to a maximum scale of 100%. When the content is zoomed and orientation changes, I want the content to fit-width again.

Edit 1: I can change meta tags in the head of HTML, but its content remains larger than the webview's visible area. I'm supporting Android 2.3 and later.

Edit 2: Thanks for the suggestion on responsive web design. I know what it is and I can do it. Problem is I cannot change much of the HTML content in this case.

TL;DR: How to always fit a larger HTML content to a Android webview's width as the webview resizes, without reloading the content. (I can change the webview, not much of the HTML.)

5
Some pictures for reference: when webview first loads, overview mode works well but when orientation changes, the page is not rendered in overview mode anymoreHoang Huynh

5 Answers

18
votes

do following settings

    webview.getSettings().setUseWideViewPort(true);
    webview.getSettings().setLoadWithOverviewMode(true);
5
votes

This may sound like a cop-out answer, but I think it depends on how you're building the rest of your site. Whenever I work on responsive sites, I use:

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

This will do two things: width=device-width will find the width of your device, and send styles from your CSS that match that size (you can set styles for specific sizes in your CSS, using media queries). Initial-scale=1 will make the site show up at 100% when it loads, but still give users the option to zoom in.

If your site is developed for a specific width, you could try using:

<meta name = "viewport" content = "width = 980 (or whatever the width you're designing for)">

This will just scale down your site to fit the device width. Nothing super special here, users will basically just be shown mini-desktop sites.

I'd recommend reading this Webdesign Tuts+ article. They explain the viewport meta tag well, with examples.

2
votes

You need not change anything on Webview , all u need is Responsive Html Page, If you are designer then that will solve ur issue or u can post a part of ur Question with UI tag.

To built Responsive html pages u can visit : HERE

Result : You need to test on webview while creating your responsive html page, default brower of android itself reflect that ur html page is responsive or not.Once it will work in default brower in Android device then it will work in any webview in android.

Testing : you can test ur responsive html page HERE

0
votes

I would have to see the source of your HTML and CSS. You have to exclusively use % sizes and never use fixed width px sizes. Then, the user can change the size of the browser and it will dynamically resize. I also use % widths because of this. It also dynamically resizes if you use CTRL-MouseWheel (zoom).

0
votes

Here's a tip for responsive web design, use positions instead of widths:

For example:

html

<div id="somediv">I'm just a div, just a regular div.</div>

css

#somediv {
left: 0; /*numeric value in pixels you would like this div to be from the left */
right: 0; /*same as above, except on the right */
height: 50px;
width: auto;
}

The div above will automatically resize based on screen width.