N
N
Nikolai2020-01-10 05:09:08
Android
Nikolai, 2020-01-10 05:09:08

How can I set the application to webview so that it only opens "native" links?

Hello. I have a pretty simple webview application. The problem is that it opens all links within the application. That is, in fact, trying to be a browser. How to define a list of links that will be opened inside the application, so that the rest are already offered to be opened in the browser, etc.?
PS I have over 30 "native" links

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2020-01-10
@niknik_ykt

public class MyWebViewClient extends WebViewClient {
  @Override
   public boolean shouldOverrideUrlLoading(WebView view,final String url) {
//try to find browse activity to handle uri
        final Uri parsedUri = Uri.parse(url);

        AlertDialog alertdialog = new AlertDialog.Builder(activity)
                .setIcon(R.mipmap.alert)
                .setTitle(Objects.requireNonNull(RawPrinterApp.getAppContext()).getString(R.string.open_url))
                .setMessage(url).setNegativeButton(Objects.requireNonNull(RawPrinterApp.getAppContext()).getString(R.string.btn_no),null)
                .setPositiveButton(Objects.requireNonNull(RawPrinterApp.getAppContext()).getString(R.string.btn_yes), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        webView.loadUrl(url);
                    }
                }).create();
        alertdialog.setButton(AlertDialog.BUTTON_NEUTRAL,Objects.requireNonNull(RawPrinterApp.getAppContext()).getString(R.string.reSelectTxt), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                PackageManager packageManager = activity.getPackageManager();
                Intent browseIntent = new Intent(Intent.ACTION_VIEW).setData(parsedUri);

                // Create intent to show chooser
                Intent chooser = Intent.createChooser(browseIntent, Objects.requireNonNull(RawPrinterApp.getAppContext()).getString(R.string.open_url));

                if (browseIntent.resolveActivity(packageManager) != null) {
                    activity.startActivity(chooser);
                }

            }
        });
        alertdialog.show();
       return true; // говорим что стандартное поведение было переопределено и сама вьюшка не дергается
   }
}

Conditions for the appearance of a dialogue and in general whether you need it or not, you will redo it yourself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question