Pankaj
Pankaj

Reputation: 861

Determine particular webview page loaded in android

We've multiple html pages (for example abc.com/abc.html, abc.com/xyz.html) to load in single WebView class of android. Now, I want to achieve that once any particular page loaded successfully, I can identify particular page and behalf of that I can some display message in native app. I was using webview.getTitle() to identify, but it's getting change some time from webview page.

Upvotes: 0

Views: 53

Answers (2)

Xavier Melendez
Xavier Melendez

Reputation: 11

If this is only the title that you want to track, you can implement a WebChromeClient and listen to the event ReceivedTitle in the method ReceivedTitle()

you can look at: WebChromeClient

myWebView.setWebChromeClient(new WebChromeClient(){
         onReceivedTitle(WebView webview, String title){
             // put your code here

         }
    }
)

EDIT

  • You are dealing with javascript so you have to enable it in your web view:

    public class MainActivity extends AppCompatActivity {
    
    private WebView myWebView;
    private TextView myTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://example.local/changetitle/");
        myTextView = (TextView) findViewById( R.id.textView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                myTextView.setText(title);
            }
        });
    }
    

    }

  • here is a sample webpage a tested it:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>First Title</title>
	</head>
	<body onload="myFunction();">
		<div id="text">No changes</div>
		<script>
            var i=27
            function myFunction() {
    
                document.title=""+i;
	            var x = document.getElementById("text");   // because title won't dispay in the body
	            x.innerText = "title : "+document.title;   
	            i++;
	            setTimeout(myFunction,3000);
            }
		</script>

	</body>
</html>

Hope it helps

Upvotes: 1

ND1010_
ND1010_

Reputation: 3841

You can compare link(URL) before load a particulate page in to WebView

webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE);

              /* compare your URL from here*/

                updateUrl(url);
            }


            @Override
            public void onPageFinished(WebView view, String url) {
//                progressDialog.dismiss();
                progressBar.setVisibility(View.GONE);
            }
        });

Upvotes: 0

Related Questions