0

enter image description here

Hi all.

Do we have any way to change the alert dialog button in webview? At the moment the alert dialog button is white and it can't be seen as the background colour is white as well.

Tried changing the webview user agent and also tried to override onJsAlert, but it does not help. I tried to debug but it does not go into onJsAlert function when the alert dialog is shown.

    mContentWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                AlertDialog dialog = new AlertDialog.Builder(view.getContext(), R.style.CustomAlertDialogStyle).
                        setTitle("YourAlertTitle").
                        setMessage(message).
                        setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //do nothing
                            }
                        }).create();
                dialog.show();
                result.confirm();
                return true;
            }

Tried accessing the website on mobile web browser and the button colour is black. Just in Android app is white colour.

Any advice on changing the alert dialog button colour?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mei Yi
  • 341
  • 3
  • 14
  • You can create custom alert dialog with your own view - check this answer here https://stackoverflow.com/a/53550564/8041528 – Pankaz Kumar Jun 15 '21 at 05:20
  • @PankazKumar Hi. The alert dialog is from the webview. Try to customize from onJsAlert but can't. – Mei Yi Jun 15 '21 at 05:54
  • Does this answer your question? [AlertDialog do not show positive and negative button](https://stackoverflow.com/questions/39481735/alertdialog-do-not-show-positive-and-negative-button) – aryanknp Jun 15 '21 at 06:20
  • @aryanknp Hi. When I try to debug it does not goes into onJsAlert function. So unable to set the theme for the webview alert dialog box. – Mei Yi Jun 15 '21 at 06:56
  • @MeiYi is `mContentWebView.setJavaScriptEnabled(true);` – aryanknp Jun 15 '21 at 07:05
  • @aryanknp Have added this. :-) – Mei Yi Jun 15 '21 at 09:05

2 Answers2

0

Solved as the prompt was onJsConfirm.

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ActivityBrowserWithoutToolbar.this);
                alertDialogBuilder.setTitle(url);
                alertDialogBuilder.setMessage(message);
                alertDialogBuilder.setPositiveButton(
                        getString(R.string.ok_button),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                result.confirm();
                            }
                        });

                alertDialogBuilder.setNegativeButton(
                        getString(R.string.cancel_button),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                result.cancel();
                            }
                        });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.setOnShowListener(arg0 -> {
                    alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.accent_color));
                    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.accent_color));
                });

                alertDialog.show();
                return true;
            }
        });
Mei Yi
  • 341
  • 3
  • 14
0

The Alert Dialog uses the activity theme, so the positive button color uses the colorAccent of the activity theme defaultly. you should check it.

if you did not set the activity theme, please check the colorAccent of your application theme.

Lenoarod
  • 3,441
  • 14
  • 25
Dali
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 29 '22 at 05:37