Home » Android » android – Custom AlertDialog not showing on UI sometimes why?-Exceptionshub

android – Custom AlertDialog not showing on UI sometimes why?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I am showing my own AlertDialog. It works and shows fine in most cases but in when I call from the following activity it does not show on UI. The code executes completely with no exceptions, I have even checked my logs and with breakpoints, Did not find any issue. But the dialog does not appear on UI. Can anyone tell me what the problem might be?

This is the method in my activity that calls the dialog.

private void showCustomDialog(final int messageType,
                                  final String titleString,
                                  final String msgString,
                                  final String specificMsgString,
                                  final int positiveButtonString,
                                  final int negativeButtonString,
                                  final Runnable positiveButtonClickHandler,
                                  final Runnable negativeButtonClickHandler) {
        if (mDialog != null && mDialog.isShowing()) {
            Log.d(TAG, "showCustomDialog: cannot display two dialogs at once.");
            return;
        }
        runOnUiThread(() -> mDialog = DialogUtil.showDialog(this, mAlertDialogBuilder, 0, messageType, titleString, msgString,
                specificMsgString, positiveButtonString, negativeButtonString, positiveButtonClickHandler, negativeButtonClickHandler));
    }

DialogUtil.showDialog method:

public static AlertDialog showDialog(
        Activity activity,
        AlertDialog.Builder builder,
        final int borderTopColorRes,
        final int iconRes,
        final String titleString,
        final String msgString,
        final String specificMsgString,
        final int positiveButtonString,
        final int negativeButtonString,
        final Runnable positiveButtonClickHandler,
        final Runnable negativeButtonClickHandler) {

    final DialogCustomViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(activity), R.layout.dialog_custom_view, null, false);
    binding.textSpecificMessage.setVisibility(View.GONE);
    if (borderTopColorRes != 0) {
        binding.viewBorderTop.setBackgroundColor(borderTopColorRes);
    } else {
        binding.viewBorderTop.setVisibility(View.GONE);
    }
    if (iconRes != 0) {
        binding.imageCustomDialogMessageType.setImageResource(iconRes);
    }
    if (titleString != null) {
        binding.textCustomDialogTitle.setText(titleString);
        binding.textCustomDialogTitle.setVisibility(View.VISIBLE);
    } else {
        binding.textCustomDialogTitle.setText("");
        binding.textCustomDialogTitle.setVisibility(View.GONE);
    }
    if (msgString != null) {
        binding.textDialogMessage.setText(msgString);
        binding.textDialogMessage.setVisibility(View.VISIBLE);
    } else {
        binding.textDialogMessage.setText("");
        binding.textDialogMessage.setVisibility(View.GONE);
    }

    binding.textSpecificMessage.setVisibility(View.GONE);
    if (specificMsgString != null) {
        binding.textSpecificMessage.setText(specificMsgString);
        binding.textDialogMessage.setOnClickListener(v -> {
            if (binding.textSpecificMessage.getVisibility() == View.VISIBLE) {
                binding.textSpecificMessage.setVisibility(View.GONE);
            } else {
                binding.textSpecificMessage.setVisibility(View.VISIBLE);
            }
        });
    }
    if (positiveButtonClickHandler != null) {
        binding.buttonPositive.setText(positiveButtonString);
        binding.buttonPositive.setOnClickListener(v -> positiveButtonClickHandler.run());
    } else {
        binding.buttonPositive.setVisibility(View.GONE);
    }
    if (negativeButtonClickHandler != null) {
        binding.buttonNegative.setText(negativeButtonString);
        binding.buttonNegative.setOnClickListener(v -> negativeButtonClickHandler.run());
    } else {
        binding.buttonNegative.setVisibility(View.GONE);
    }

    builder.setView(binding.getRoot());
    AlertDialog dialog = builder.create();
    try {
        dialog.show();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    } catch (Exception e) {
        Log.e(TAG, "showDialog: Exception=" + e.getMessage());
        e.printStackTrace();
    }
    return null;
}
How to&Answers: