MainActivity.java
package com.cfsuman.androidtutorials;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the widgets reference from XML layout
mWebView = findViewById(R.id.webView);
// Request to render the web page
renderWebPage("https://www.google.com");
}
// Custom method to render a web page
protected void renderWebPage(String urlToRender){
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
// Do something on page loading started
}
@Override
public void onPageFinished(WebView view, String url) {
// Do something when page loading finished
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(
WebView view, int newProgress) {
}
});
// Enable the javascript
mWebView.getSettings().setJavaScriptEnabled(true);
// Enable zoom
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(true);
// Render the web page
mWebView.loadUrl(urlToRender);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC">
<WebView
android:id="@+id/webView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>