In Android, WebView is a view used to display the websites in the application. This class is the basis upon which you can create your own web browser or simply use it to display some online content within your Activity/App. We can also specify HTML strings and can show them inside our application using a WebView. Basically, WebView turns a web application into an android application.
First of all, edit your AndroidManifest file and add Internet permission in the app because we are going to access the Online content from the internet.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
activity_web_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@android:color/darker_gray"
tools:context=".Web_View">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Website URL"
android:textColorHint="@android:color/darker_gray"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_marginTop="20dp"
android:paddingStart="20dp"
android:paddingEnd="10dp"
android:id="@+id/site"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search"
android:textColorHint="@android:color/darker_gray"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_marginTop="20dp"
android:paddingStart="20dp"
android:paddingEnd="10dp"
android:layout_below="@+id/site"
android:id="@+id/search"
/>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColorHint="@android:color/darker_gray"
android:layout_marginTop="20dp"
android:paddingStart="20dp"
android:paddingEnd="10dp"
android:layout_below="@+id/search"
android:id="@+id/webView"
/>
</RelativeLayout>
Web_View.java
package com.freetoreads.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Web_View extends AppCompatActivity {
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
Button btn = findViewById(R.id.search);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = findViewById(R.id.site);
String url = editText.getText().toString();
url = "https://" + url;
WebView mWebView = findViewById(R.id.webView);
mWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl(url);
}
});
}
}
So in this way, we can create WebView Android App easily. If you face any issue/error in the above code please comment below I will reply to you as soon as possible. Thank You
Tags:
Android