Thursday, 31 October 2013

Webview Demo in android

Today,we look into a web-view to open a simple link without using browser.

1)Add the web-view in your XML with the following.

        <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

2)Then write the following code in your activity to display the link in your web-view.

        WebView web;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

web = (WebView) findViewById(R.id.webView1);

web.setWebViewClient(new myWebClient());
web.loadUrl("http://uniqueandroidtutorials.blogspot.in");
}

public class myWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub

view.loadUrl(url);
return true;

}
}

// To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

Done..Thanks..

To download the demo Click Here..

0 comments:

Post a Comment

 

Copyright © Unique Android Tutorial.. Design by Free CSS Templates | Blogger Theme by BTDesigner | Powered by Blogger