3

I am opening a url in android webview which is redirecting the user to google play store web page in android webview and play store page is showing OPEN IN PLAY STORE APP

enter image description here

and while clicking that button rather then opining play store app webview is redirecting to a web url which is not found.

enter image description here

Now i don't know why it is happening as in google chrome app same scenario is working fine. Please suggest the better solution.

Some code i have done :

   mWebView.loadUrl("https://play.google.com/store/apps/details?id=in.org.npci.upiapp&hl=en") 

   mWebView.setWebViewClient(new WebViewClient() {


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

        }

        public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
            return super.shouldOverrideUrlLoading(view, urlNewString);
        }

        @Override
        public void onPageFinished(WebView view, String url) {

        }
    });

Note: I am using this play store url just as an example.

Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65
  • Not sure if this will help but try setting Webchromeclient `mWebView.setWebChromeClient(new WebChromeClient());` – Manohar Apr 16 '19 at 10:40
  • @ManoharReddy no its not working – Kapil Rajput Apr 16 '19 at 10:54
  • Why not directly open your app on play store https://stackoverflow.com/questions/11753000/how-to-open-the-google-play-store-directly-from-my-android-application ? – Manohar Apr 16 '19 at 11:14
  • @ManoharReddy i have to open a web url in a `webview` and that page contains some buttons like download this app from google play store so i cant open the app on play store directly – Kapil Rajput Apr 16 '19 at 12:05
  • I wouldn't use Google Play as an example. Google Play has specific intent features so if you navigate to it on Android it will open the Play Store app instead. Try with a different URL. – Nick Fortescue Apr 16 '19 at 16:00
  • @NickFortescue i tried with different url's app store app is not opening through `webview`, please try it at your end and if something i am missing please guide me – Kapil Rajput Apr 17 '19 at 04:40

2 Answers2

1

You need to override shouldOverrideUrlLoading, but you must also inspect the URL and conditionally create an intent if it starts with the intent:// scheme. We (the Android WebView team) have written a sample application which does precisely this.

nfischer
  • 181
  • 3
0

Just put this data in the webview:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url != null && url.startsWith("https://play.google.com/store/apps/developer?id=putyourplaystoreid")) {
        Intent i = new Intent("android.intent.action.VIEW");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setData(Uri.parse(url));
        startActivity(i);

        return true;
    } else {
        return false;
    }
}
Elikill58
  • 4,050
  • 24
  • 23
  • 45