Get Show loaded webpage Title and URL from WebView in android app’s ActionBar

How to get Title and URL from android.webkit.WebViewClient and show inside app’s ActionBar TitleBar.

In this tutorial we are making simple android application with the combination of WebView in it but we are covering more important topic inside this project which is Getting current opening URL and Webpage Title( Which we will open inside android app using WebView ) and display that Title + URL into Action bar like we have already seen inside every android web browser. So here is the complete step by step tutorial for Get Show loaded webpage Title and URL from WebView in android.

android-project-download-code-button

How to Get Show loaded webpage Title and URL from WebView in android app’s ActionBar.

Note :  Please add Internet permission inside your AndroidManifest.xml file.

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

Code for MainActivity.java file.

 package com.getwebviewpagetitleurl_android_examples.com;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends Activity {

 WebView webView;
 ProgressDialog Pdialog;
 String PageURL, PageTitle ;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 webView = (WebView)findViewById(R.id.webView1);
 
 webView.setWebViewClient(new WebViewClient());
 
 webView.loadUrl("https://www.android-examples.com");
 
 }
 
 public class WebViewClient extends android.webkit.WebViewClient
 {
 @Override
 public void onPageStarted(WebView view, String url, Bitmap favicon) {

 // TODO Auto-generated method stub
 super.onPageStarted(view, url, favicon);
 
 PageURL = view.getUrl();
 
 getActionBar().setSubtitle(PageURL);
 
 }
 
 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) {

 // TODO Auto-generated method stub
 view.loadUrl(url);
 return true;
 }
 @Override
 public void onPageFinished(WebView view, String url) {

 // TODO Auto-generated method stub

 super.onPageFinished(view, url);
 
 //Getting WebPage URL .
 PageURL = view.getUrl();
 
 //Getting WebPage TITLE .
 PageTitle = view.getTitle();
 
 //Adding Page URL inside Action Bar;
 getActionBar().setTitle(PageURL);
 
 //Adding Page Title inside Action Bar;
 getActionBar().setSubtitle(PageTitle);

 }

 }
 
 }

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.getwebviewpagetitleurl_android_examples.com.MainActivity" >

 <WebView
 android:id="@+id/webView1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" />

</RelativeLayout>

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.getwebviewpagetitleurl_android_examples.com"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="14"
 android:targetSdkVersion="21" />
 
 <uses-permission android:name="android.permission.INTERNET" />

 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>

Screenshots:

webview-title

Get Show loaded webpage Title and URL from WebView in android app's ActionBar

Click here to download Get Show loaded webpage Title and URL from WebView in android app’s ActionBar project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *