How to check if webView has loaded the following page in Android?

When the app is opened, a web browser is opened that says "www.google.com". If the user enters a query and clicks Search (in the WebView itself, not the Android button), the next page is loaded. Now I need to know if the webView has loaded the following page which is the search result for the query.

Is there a built-in method to test this?

EDIT 1

Implementation of the Asuthosh Panda and Chintan Soni method. It still doesn't work. This is implemented in the snippet. Does this affect the code in any way?

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    String url;
    //String urrrl=null;
    View view =  inflater.inflate(R.layout.fragment_net_con, container, false);
    myWebView = (WebView)view.findViewById(R.id.webView21);

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("https://www.google.com");
    myWebView.setWebViewClient(new WebViewClient());
    url=myWebView.getUrl().toString();
    Log.d("My app msg","I am here");
    Log.d("URL ",url);
    if(url.contains("q=")){
        System.exit(1);
    }
    activityCommander.setwebs(myWebView.getUrl());

    return view;    }

      

+3


source to share


3 answers


You can set the callback with WebViewClient

as below:



mWebView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        // do your stuff here
    }
});

      

+2


source


I don't know what you really ask

So I am using the Simple Browser project code

Hoping this will help you

First you need to define the WebView client class

import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MyWebClient extends WebViewClient{
@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

    view.loadUrl(url);

    return true;
 }
}

      



Second, Editing Main Activity

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


public void sendGo(View view) {
    Intent intent = new Intent(this,GoActivity.class);
    startActivity(intent);
 }

}

      

Third, GGoActivity like this

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class GoActivity extends Activity {

WebView myWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_go);

    myWebView = (WebView) findViewById(R.id.webview_go);

    myWebView.loadUrl("http://www.google.com");

    myWebView.setWebViewClient(new MyWebClient());

    WebSettings webSettings = myWebView.getSettings();

    webSettings.setJavaScriptEnabled(true);
 }
}

      

Happy coding

+4


source


  • For the WebView, you can get the URL of the page you are currently on when the page has finished loading. See the answer posted by @Chintan Soni

  • For each search query, the Google URL that uses the Get method to submit the search query will look something like this:

    https://www.google.co.in/?gfe_rd=cr&ei=psUGWeHZKe3I8Af2rL7YAg#q=android

    where q is the key for the query (your search string). Here I was looking for "android".

    So, if your URL contains "q =", it means that you were looking for something and are on the search results page.

So your code should look something like this:

if(url.contains("q=")){
 // its the search page and not the initial Google home page
}

      

+1


source







All Articles