In this tutorial we are going to discuss about How to create webView in Android ?
At first create an empty project in android studio .
1 . activity_main.xml
Here we create a button name as “Open Browser” and webView inside our activity_main.xml .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Browser"/>
<WebView
android:id="@+id/wv"
android:layout_width="match_parent"
android:layout_height="wrap_content"></WebView>
</LinearLayout>
2. MainActivity.java
Now we find the id of btn and webView inside our MainActivity.java file.
package com.example.root.webview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
WebView wv;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv=(WebView)findViewById(R.id.wv);
btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("https://www.google.com/");
bt.setVisibility(View.GONE);
}
});
}
}
3. Android Manifest
we add Internet uses permission inside our AndroidManifest.xml file .
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.root.webview">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN"/>
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Now we run the project in android mobile or Emulator.