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


As an Amazon Associate I earn from qualifying purchases.

My favorite quotations..


“A man should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.”  by Robert A. Heinlein

"We are but habits and memories we chose to carry along." ~ Uki D. Lucas


Popular Recent Articles