How to get loaded web page title in Android WebView?

前端 未结 7 726
我在风中等你
我在风中等你 2020-12-01 02:00

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 i

7条回答
  •  不思量自难忘°
    2020-12-01 02:42

    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

提交回复
热议问题