Simple Android Web View

... by Bittle in Programming Help August 18, 2018

Question asked through contact form:

I want to develop a WebView android app with android studio but facing problems.

The question isn't very specific, so I will just make a simple WebView that starts up here (kodecentral.com).


First make sure that you have android studio up and running, and create a new empty activity.


Open to your main activity which is under app->java->package.name->MainActivity.java, and also open your main activity layout which is under app->res->layout->activity_main.xml.


In your activity layout change your layout to RelativeLayout and replace the TextField with a WebView, resulting in the following xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

The WebView needs an id since we will need it to display content later. The WebView's width and height match the parent, which takes up the whole screen. 


Now we need to work on the main activity.

public class MainActivity extends AppCompatActivity {
  private WebView webView;

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

    // set the webView from the id that we
    // set in activity_main.xml
    webView = findViewById(R.id.webview);

    // don't open in default browser (such as chrome), open in this app
    webView.setWebViewClient(new WebViewClient());

    // load kodecentral.com on start up
    webView.loadUrl("https://kodecentral.com");
     
    // enable js which makes most website function properly
    webView.getSettings().setJavaScriptEnabled(true);
  }
}

And after the view and main activity are complete, next you must grant internet permission to this app. Go to app->manifests->AndroidManifest.xml and grant the app permission to access the internet:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.bittle.webviewrequest">

  <uses-permission android:name="android.permission.INTERNET"/>
  ...

And there you go, The app is all set up and ready to view all the internet content you'd like.


Bonus

The app works fine, but what about making the back button take you to the previous request, instead of closing the app? Good idea, for this we have to override the onBackPressed function the following way:

@Override
public void onBackPressed() {
  if (webView.canGoBack()) {
    // go back to previous page if it exists
    webView.goBack();
  } else {
    // if no prev page, just execute the default action
    super.onBackPressed();
  }
}

Comments (0)

Search Here