Friday, January 22, 2016

android - How to get WebView loaded page title and url

MainActivity.java

package com.cfsuman.androidtutorials;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Objects;


public class MainActivity extends AppCompatActivity {
        private MainActivity mContext;
        private WebView mWebView;
        private String mTitle = "";
        private String mUrl = "";

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

                // Get the activity
                mContext = MainActivity.this;

                // Get the widgets reference from XML layout
                Button button = findViewById(R.id.button);
                mWebView = findViewById(R.id.webView);


                // Set a click listener for button widget
                button.setOnClickListener(view -> {
                        // Request to render the web page
                        renderWebPage("https://www.android--code.com/?m=1");
                });
        }



        // 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
                                showToast("Page loading started");

                                // Only url is available in this stage
                                mUrl = view.getUrl();

                                // Update the action bar
                                Objects.requireNonNull(getSupportActionBar())
                                        .setSubtitle(mUrl);
                        }

                        @Override
                        public void onPageFinished(WebView view, String url){
                                // Do something when page loading finished
                                showToast("Page loaded");

                                // Url and title are available at this stage
                                mUrl = view.getUrl();
                                mTitle = view.getTitle();

                                // Update the action bar
                                Objects.requireNonNull(getSupportActionBar())
                                        .setTitle(mTitle);
                                getSupportActionBar().setSubtitle(mUrl);
                        }

                });


                // Enable the javascript
                mWebView.getSettings().setJavaScriptEnabled(true);

                // Render the web page
                mWebView.loadUrl(urlToRender);
        }



        // Method to show toast message
        private void showToast(String message){
                Toast.makeText(mContext,message,Toast.LENGTH_SHORT).show();
        }
}
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">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="Load URL"
        app:layout_constraintStart_toStartOf="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/button" />

</androidx.constraintlayout.widget.ConstraintLayout>