1

I'm creating an application with 3 tabs containing webview. The WebViews are loading different urls such as Amazon, Flipkart and Myntra. I want to search in the respective sites. For that I'm using an EditText to get the keyword. The text from EditText(keyword) is passed to the fragments and url should be changed accordingly(such as https://www.amazon.com/s/?field-keywords=keyword). The problem arises when the url is loading. Url is not changing even after concatenating the url with the keyword. The fragments are loading the same url everytime. Here is the fragment code.

onButtonClick method

    //calling the Adapter again
    String value = et.getText().toString();
    webViewAdapter = new WebViewAdapter(getSupportFragmentManager(),this,value);
    vp.setAdapter(webViewAdapter);

Adapter code

@Override
public Fragment getItem(int i) {
    Bundle args = new Bundle();
    args.putString("search_term",key);
    switch (i)
    {
        case 0:
            AmazonFragment amazonFragment = new AmazonFragment();
            amazonFragment.setArguments(args);
            return amazonFragment;
        case 1:
            //similar to case 1
        case 2:
            //similar to case 1
    }
    return null;
}

Fragment code

    String url = "https://www.amazon.com/s/?field-keywords=";
    Bundle args = getArguments();
    keyword = args.getString("search_term");
    if(keyword == null)
        keyword = "";
    View v = inflater.inflate(R.layout.webview_fragment,container,false);
    webView = v.findViewById(R.id.webview);
    url =  url.concat(keyword);
    webView.setWebViewClient(new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed(); // Ignore SSL certificate errors
        }
    });

    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.loadUrl(url);

Please help.

Dev Sharma
  • 597
  • 5
  • 22
  • you need to post the code calling the fragment and sending the keyword to it as well, the fragment seems ok – kkarakk Oct 22 '18 at 18:40
  • @KaranHarshWardhan I've edited the code. Have a look. – Dev Sharma Oct 22 '18 at 18:57
  • Possible duplicate of [Android App development - WebView is not working](https://stackoverflow.com/questions/5431577/android-app-development-webview-is-not-working) – kkarakk Oct 22 '18 at 19:20
  • same issue as in linked question. you're overriding url loading,returning true and then trying to load url - webview.loadurl(url), meaning url will never load – kkarakk Oct 22 '18 at 19:21
  • If I remove WebViewClient, the url loads in browser. What should I do? – Dev Sharma Oct 22 '18 at 19:39
  • https://stackoverflow.com/questions/31159149/using-webview-in-fragment is what you should do, basically just removed your override of url loading,keep everything else the same – kkarakk Oct 22 '18 at 19:43

2 Answers2

0

Much simpler way to do this would be to have an interface callback from the activity to the fragments with the value and updating webviews in fragments with modified data

String URL = existingUrl + searchValue;
webView.loadUrl(URL)
Naimish Srivastava
  • 343
  • 1
  • 3
  • 14
  • yup, this is cleaner but @dev might need to do more customization of his fragments for each website – kkarakk Oct 22 '18 at 19:45
  • But without the WebViewClient it asks for a browser to load the website. – Dev Sharma Oct 22 '18 at 19:47
  • This is only the part about updating the url. Don't remove the webviewClient code, set that too like this https://stackoverflow.com/a/4066497, remember to return false in shouldOverrideUrlLoading – Naimish Srivastava Oct 22 '18 at 19:51
0

Removed your override of urlloading an it should work

 Bundle args = getArguments();
    keyword = args.getString("search_term");
    if(keyword == null)
        keyword = "";
    View v = inflater.inflate(R.layout.webview_fragment,container,false);
    webView = v.findViewById(R.id.webview);
    url =  url.concat(keyword);
    webView.setWebViewClient(new WebViewClient(){
    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed(); // Ignore SSL certificate errors
    }
});

webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(url);
kkarakk
  • 1,026
  • 7
  • 21