How to set the initial zoom/width for a webview
I figured out why the portrait view wasn't totally filling the viewport. At least in my case, it was because the scrollbar was always showing. In addition to the viewport code above, try adding this:
browser.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
browser.setScrollbarFadingEnabled(false);
This causes the scrollbar to not take up layout space, and allows the webpage to fill the viewport.
Hope this helps
The following code loads the desktop version of the Google homepage fully zoomed out to fit within the webview
for me in Android 2.2 on an 854x480 pixel screen. When I reorient the device and it reloads in portrait or landscape, the page width fits entirely within the view each time.
BrowserLayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Browser.java:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Browser extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.BrowserLayout);
String loadUrl = "http://www.google.com/webhp?hl=en&output=html";
// initialize the browser object
WebView browser = (WebView) findViewById(R.id.webview);
browser.getSettings().setLoadWithOverviewMode(true);
browser.getSettings().setUseWideViewPort(true);
try {
// load the url
browser.loadUrl(loadUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
It is and old question but let me add a detail just in case more people like me arrive here.
The excellent answer posted by Brian is not working for me in Jelly Bean. I have to add also:
browser.setInitialScale(30);
The parameter can be any percentage tha accomplishes that the resized content is smaller than the webview width. Then setUseWideViewPort(true) extends the content width to the maximum of the webview width.