Immanuel

Tuesday, October 16, 2012

Sclae Web Page according to the Device Size - Android 4.0 WebView

Web pages can be opened from the application using the web view. The actual problem come when you open the web pages in different screen. It is not a good idea to create the page or a application for every devices. So what to do ...?

It is possible to scale the web pages in web view it self now lets see how .

Create the web view layout in res/layout.home.xml


android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webview">


Inflate this in a java file

WebView webview;
setContentView(R.layout.home);
webview = (WebView) findViewById(R.id.webview);
webview.loadUrl("file:///android_asset/IT/index.html");
webview.getSettings().setJavaScriptEnabled(true);

This will open the web page that is not properly fit into the screen so the solution is very simple just add three more lines.

webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setDomStorageEnabled(true);

Happy Hacking..

No comments:

Post a Comment

Twitter Bird Gadget