5e. Android: opening a Web page

In this Tutorial you will learn how to open a Web page in Android.

Step 1: Add WebView to the layout/activity_our_website.xml

    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />


Step 2: Add code to handle opening of the Web page

public class OurWebsite extends ActionBarActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_our_website);
      WebView myWebView = (WebView) findViewById(R.id.webview);
      WebSettings webSettings = myWebView.getSettings();
      webSettings.setJavaScriptEnabled(true);

      String url = "https://plus.google.com/106135277932130159833/posts";
      if (url.contains("plus.google")) {
         // for better experience we give user a choice of app to handle the URL
         // e.g. Google Plus vs. Chrome
         Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
         startActivity(i);

      }
      else {
         // open as default WebView
         myWebView.loadUrl(url);
      }
   }




















Read more:
http://developer.android.com/guide/webapps/webview.html

No comments:

Post a Comment