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 (

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->>, 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=""


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;

  protected void onCreate(Bundle savedInstanceState) {

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

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

    // load on start up
    // enable js which makes most website function properly

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=""

  <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.


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:

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

Comments (0)

Search Here