How to get WebView Loaded Page Title and URL in Android

How to get WebView Loaded Page Title and URL in Android

We are working on android browser app for one of our clients. In the browser, we need to display current page URL. In our demo app, we are going to display page loaded URL in an EditText. Before start below is the demo app.   For this app, we need to Internet permission. Add below line in our AndroidManifest.xml file.

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

As shown in the video in our layout we have an EditText, Button to Load URL and a WebView to display. Below is the code for activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.vlemonn.java.blog.webviewloadedpagetitleandurl.MainActivity">

    <EditText
        android:id="@+id/etLoadURL"
        style="@style/Base.V7.Widget.AppCompat.EditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="https://androidtutorialfun.com"
        app:layout_constraintEnd_toStartOf="@+id/btnLoadURL"
        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnLoadURL"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="GO"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <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_toBottomOf="@+id/etLoadURL">

    </WebView>

</android.support.constraint.ConstraintLayout>

Below is the code for MainActivity.java class file

package com.vlemonn.java.blog.webviewloadedpagetitleandurl;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    Button btnLoadURL;
    EditText etLoadURL;
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnLoadURL = findViewById(R.id.btnLoadURL);
        etLoadURL = findViewById(R.id.etLoadURL);
        webView = findViewById(R.id.webView);

        btnLoadURL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadURL(etLoadURL.getText().toString());
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                btnLoadURL.setText(progress+"%");
            }
        });
        webView.setWebViewClient(new MyWebViewClient());
        WebSettings browserSetting = webView.getSettings();
        browserSetting.setJavaScriptEnabled(true);
        loadURL("https://androidtutorialfun.com");


    }

    private void loadURL(String url) {
        webView.loadUrl(url);
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            btnLoadURL.setText("GO");
            // Update EditText and Action bar
            getSupportActionBar().setTitle(view.getTitle());
            getSupportActionBar().setSubtitle(view.getUrl());
            etLoadURL.setText(view.getUrl().toString());

        }
    }
}

Let's run our app. Below is the different screen.

0 Comments
Leave A Comment

Please login to post your valuable comments.

Join the newsletter

Get the latest vLemonn news first

share