Skip to content Skip to sidebar Skip to footer

Create first webview in android studio

Webview is a tool in android studio and it is used to load HTML or website to mobile, through this we can easy to create the android application through HTML, CSS, and javascript code.

1) First create a new project in android studio with more details...

2) Open the activity_main.xml file under the layout folder.

Open layout.xml file in android studio


3) Copy the following code to the activity_main.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"
/>

xml code for creating webview


     When you add the code to activity_main.xml you can see the preview by clicking Design.


Webview Preview in design tab

4) You must set internet permission in AndroidManifest.xml. Open the AndroidManifest.xml file and enter the following code.

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

add android.permission.Internet user permission in android

5) Open the MainActivity.java file and modify it by the below steps.
  
    a) import the following class
import android.webkit.WebView;
import android.webkit.WebViewClient;

    b) When you want to load the URL to your webview, just call the below code.
        WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.google.com");


Java code for webview

6) Now you are done with your first webview, just run the code.


Java code for webview final output





Post a Comment for "Create first webview in android studio"