1

I am trying to get the title of a webpage in a webView, I cannot just call webView.getTitle(); because even in onPageStarted() the WebView has not yet received the title. I do however already have the URL for the webpage that being loaded, so if theres something like getTitle(url); That would be exactly what I want.

Jonik
  • 80,077
  • 70
  • 264
  • 372
timeshift117
  • 1,720
  • 2
  • 18
  • 21

2 Answers2

2

Just using the URL, you'd have to load the document over the network, parse it, and then take the title—which you probably don't want to do yourself.

I think what you actually need is this: set a custom WebViewClient for your WebView, and implement onPageFinished() for that; the WebView instance passed to that method has title set. This answer has a complete example.

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
  • yeh, I am already using onPageFinished() for this, but its too slow, I wanted the title as soon as the page started loading. I may just parse the html myself, thanks anyway. – timeshift117 Feb 15 '14 at 11:02
  • @timeshift117: You could try WebChromeClient and its `onReceivedTitle()` which [according to this answer](http://stackoverflow.com/a/12154530/56285) is quicker. – Jonik Feb 15 '14 at 11:07
1

Its not the best way but it works(Its about 70% faster):

    web.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) 
         {  

            if(progress > 30 && progress < 41){
            // using boolean:lock to call this method once everytime
             if(!lock){  
                // get the Title by web.getTitle();
             lock = true;
            }                    
            }

            if(progress > 40){ lock = false;    }

  }});

OR another way(Even Faster) :

    web.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) 
         {  

            if(OldPageTitle != web.getTitle()){//getTitle has the newer Title 
            // get the Title
            OldPagerTitle = web.getTitle();
            }                    



  }});