Do you know that many apps these days are just JS based? Some apps are fully JS apps while some other apps just embed some webpages into it, even Facebook and Facebook Page are doing it. Well, it is easy and convenient to do. So in this tutorial, I will teach you how embed a webpage into your Navigation Drawer by teaching you to how to use WebView in Fragment in the the latest Android Studio to date.

Here Is A Method That Is Helping How to Use WebView in Fragment in Android Studio

1. Create an Android project and call it WebViewFragment. And my company domain is

Then, choose Navigation Drawer Activity

Finally it generates a lot of files:

2. After it built, it generates a lot of files. But you open content_main.xml and change this code:

The most important here in this code is that you need to give a name to a layout because that is the place that a Fragment will go into. In this case, I named it mainLayout in the inner LinearLayout. You can see the code:


Please note that tools:context=”com.kosalgeek.webviewfragment.MainActivity” includes my project name and company name. You should check your project name and company name and change them here.

3. Create a Fragment, called WebFragment1

Then, name WebFragment1. Make sure to uncheck both checks as you see the screen below:

You should add a WebView in the Fragment as you can see in the screenshot below:

And here is source code of the fragment_web_fragment1.xml layout.

Make sure you give the id to WeView as wvPage1

4. Add INTERNET Permission

Open AndroidManifest.xml and add INTERNET Permission:

5. Add Fragment Calling Code

Here is the full source code of the

6. Add WebView Code

Here is the main code to retrieve a webpage. So go to the WebFragment1 again:

OK, let me break down the code for you. Let’s look at this part:

This line View v = inflater.inflate(R.layout.fragment_web_fragment1, container, false); is important for the Fragment to let you call v.findViewById() method. Unlike in a normal Activity, in Fragment you need to use the inflater class to inflate the fragment on your Activity.

After that line, you will be able to call loadUrl() method with your url. And WebSettings for the Javascript enabler. And after that, you need to set a WebViewClient.

There are some changes for Android N, you need some additional code for Android N:

You can copy and paste the MyWebViewClient because you won’t change anything here.