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

2) Open 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 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 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





0 nhận xét:

Đăng nhận xét