user1092153
user1092153

Reputation:

Getting web page title from setWebViewClient?

i am using webview in my android app. i want to get title of current page shown in webview. i am using following code to do that

webView.setWebViewClient(new WebViewClient(){
    public void onPageFinished(WebView view, String url) {
        TextView t=(TextView)findViewById(R.id.title); 
        t.setText(view.getTitle());
    }
}

This code works but not always. Sometimes it doesn't show the title. sometimes it shows title of previous page. Whats wrong here??

Upvotes: 4

Views: 5008

Answers (2)

Zeba
Zeba

Reputation: 3051

The WebChromeClient will give you the web page title faster than the WebViewClient

webview.loadUrl("https://www.google.co.in/");
webview.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onReceivedTitle(WebView view, String sTitle) {
        super.onReceivedTitle(view, sTitle);
        if (sTitle != null && sTitle.length() > 0) {
            title.setText(sTitle);
        } else {
            title.setText("Web Page");
        }
    }
});

Upvotes: 7

Vishwanath.M
Vishwanath.M

Reputation: 6317

Check this code, am getting web page title

webview.loadUrl("https://www.google.co.in/");
webview.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        String name = webview.getTitle();
        Log.v("Title",name);
    }
});

Upvotes: 2

Related Questions