49

I'm using a WebView in my application, I have a requirement to change the app title based on the page user is on. How can I do this in Android WebView?

I did this in iphone by following line

self.title = [webPage stringByEvaluatingJavaScriptFromString:@"document.title"]

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Adds Progrss bar Support
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.webview );

        // Makes Progress bar Visible
        getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 

        mWebView = (WebView) findViewById( R.id.webview ); //This is the id you gave 
        //to the WebView in the main.xml
        mWebView.getSettings().setJavaScriptEnabled(true);   
        mWebView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this 
        //if ROM supports Multi-Touch      
        mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM

        // Load URL
        Bundle b = getIntent().getExtras();
        String url = b.getString("url");
        Log.d(TAG, "url " + url);
        mWebView.loadUrl(url);


        // Sets the Chrome Client, and defines the onProgressChanged
        // This makes the Progress bar be updated.
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress){
                //Make the bar disappear after URL is loaded, and changes string to Loading...
                myActivity.setTitle("Loading...");
                myActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

                // Return the app name after finish loading
                if(progress == 100)
                    myActivity.setTitle(R.string.app_name);
            }
        });

        mWebView.setWebViewClient(new WebViewClient(){

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

            @Override
            public void onLoadResource(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onLoadResource(view, url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                ImageView logoImageView = (ImageView)findViewById(R.id.logoimage);
                logoImageView.setVisibility(View.GONE);
                Log.d(TAG, "view.getTitle() " + view.getTitle());
                myActivity.setTitle(view.getTitle());
            }

        });

    }
Jonik
  • 80,077
  • 70
  • 264
  • 372
Sam
  • 6,215
  • 9
  • 71
  • 90

7 Answers7

113

You'll have to use a custom WebViewClient to get this done. You will override the onPageFinished() method so when a new page finishes loading you can set the webview to the appropriate title.

Below is a sample implementation of the above:

    WebView mWebView = (WebView) findViewById(R.id.mwebview);
    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            ExperimentingActivity.this.setTitle(view.getTitle());
        }
    });

You're a going to do that where you're initializing your webview. Replace the "ExperimentingActivity" to whatever you activity's name is.

If you're already overriding the WebViewClient, just add this function or the code inside to your already existing function.

You can get more info on the classes and functions I'm using here at:

Android Developers: Activity - setTitle()

Android Developers: WebViewClient

Android Developers: WebView

DallaRosa
  • 5,737
  • 2
  • 35
  • 53
  • Sure, no problem. Just take a look through the documentation links I posted and you'll get a grasp of the stuff you can do with the WebView class and the WebViewClient. It might look complicated at first but it's gives you a lot of flexibility. – DallaRosa Nov 19 '11 at 10:52
  • i can read the value but when i set the value it's not showing in the app title, i have update the question with the my web view implementation.please help – Sam Nov 19 '11 at 12:11
  • Look at your code. You're setting a custom webviewclient and a custom webchromeclient after loading the page. You have to move the "mWebView.loadUrl(url);" line to the end of the onCreate method (or at least after setting the webviewclient and webchromeclient) for those settings to work – DallaRosa Nov 19 '11 at 13:55
53

My observation is that, you get the webpage title using WebChromeClient in lesser time than using WebViewClient

webview.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onReceivedTitle(WebView view, String title) {
        super.onReceivedTitle(view, title);
        if (!TextUtils.isEmpty(title)) {
            YourActivity.this.setTitle(title);
        }
    }
});
Jonik
  • 80,077
  • 70
  • 264
  • 372
Zeba
  • 3,041
  • 1
  • 28
  • 39
  • 2
    This doesn't seem to be called after WebView.goBack() is called. So, if you need to handle back navigation I think onPageFinished() might be a better choice. – Michael Levy Apr 21 '15 at 16:36
  • 2
    See https://code.google.com/p/android/issues/detail?id=66337. It looks like onReceivedTitle() behavior was broken in some Android builds. – Michael Levy Apr 24 '15 at 16:19
3

Simplest way to read title of page in Kotlin :

      webView.webViewClient = object : WebViewClient() {
         //....
          override fun onPageFinished(view: WebView, url: String) {
           val title = view.title
            }
           //...
          }
Hamed Jaliliani
  • 2,789
  • 24
  • 31
1

So my experience you should use onReceivedTitle and onPageFinished both. I find that sometimes onPageFinished you won't get notify if you front end using react-router or something similar. And onReceivedTitle has flaw as @michael-levy mentioned.

Geng Jiawen
  • 8,904
  • 3
  • 48
  • 37
1
private WebViewClient mWvClient = new WebViewClient() {

        @Override
        public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {

            String title = view.getTitle();//getTitle
            super.doUpdateVisitedHistory(view, url, isReload);
        }
    }
taras
  • 6,566
  • 10
  • 39
  • 50
Tamic
  • 81
  • 1
  • 1
1

What if you do not want the whole title, and just a Domain Name, Like if you want to show only 'Google' from www.google.com, and you have all the query parameters in this title.

    URL hostUrl= new URL(webView.getUrl());
    String title = hostUrl.getHost();
    title = title.startsWith("www.") ? title.substring(4) : title;
Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55
0

get image and title from webview

Image

img.setImageBitmap(webview1.getFavicon());

Title

txt.setText(webview1.getTitle());