1

I'm using Java to develop Android apps. In my current app, I want to create a custom alert dialog based on MaterialAlertDialogBuilder to reuse across the app and reduce repetitive code. The problem is that I don't see the single choice or multiple choice buttons on the dialog. Here is how the dialog looks:

dialog

this is the dialog configuration :

public class CustomDialogFragment extends DialogFragment {

    private static final String ARG_TITLE = "title";
    private static final String ARG_MESSAGE = "message";
    private static final String ARG_POSITIVE_BUTTON_TEXT = "positive_button_text";
    private static final String ARG_NEGATIVE_BUTTON_TEXT = "negative_button_text";
    private static final String ARG_SINGLE_CHOICE_ITEMS = "single_choice_items";
    private static final String ARG_MULTIPLE_CHOICE_ITEMS = "multiple_choice_items";

    private String title;
    private String message;
    private String positiveButtonText;
    private String negativeButtonText;
    private String[] singleChoiceItems;
    private String[] multipleChoiceItems;
    private boolean isSingle;
    private boolean isMultiple;

    private CustomDialogViewModel customDialogViewModel;

    public static CustomDialogFragment newInstance(String title, String message, String positiveButtonText, String negativeButtonText,
                                                   String[] singleChoiceItems, String[] multipleChoiceItems) {
        CustomDialogFragment fragment = new CustomDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        args.putString(ARG_MESSAGE, message);
        args.putString(ARG_POSITIVE_BUTTON_TEXT, positiveButtonText);
        args.putString(ARG_NEGATIVE_BUTTON_TEXT, negativeButtonText);
        args.putStringArray(ARG_SINGLE_CHOICE_ITEMS, singleChoiceItems);
        args.putStringArray(ARG_MULTIPLE_CHOICE_ITEMS, multipleChoiceItems);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString(ARG_TITLE);
            message = getArguments().getString(ARG_MESSAGE);
            positiveButtonText = getArguments().getString(ARG_POSITIVE_BUTTON_TEXT);
            negativeButtonText = getArguments().getString(ARG_NEGATIVE_BUTTON_TEXT);
            singleChoiceItems = getArguments().getStringArray(ARG_SINGLE_CHOICE_ITEMS);
            multipleChoiceItems = getArguments().getStringArray(ARG_MULTIPLE_CHOICE_ITEMS);
        }

        customDialogViewModel = new ViewModelProvider(requireActivity()).get(CustomDialogViewModel.class);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireActivity());
        builder.setTitle(title);

        if (message != null) {
            builder.setMessage(message);
        }

        // Log to check if the items are passed correctly
        Log.d("CustomDialogFragment", "Single choice items: " + Arrays.toString(singleChoiceItems));
        Log.d("CustomDialogFragment", "Multiple choice items: " + Arrays.toString(multipleChoiceItems));

        if (singleChoiceItems != null && singleChoiceItems.length > 0) {
            builder.setSingleChoiceItems(singleChoiceItems, -1, (dialog, which) -> customDialogViewModel.onSingleChoiceSelected(which));
            isSingle = true;
        }

        if (multipleChoiceItems != null && multipleChoiceItems.length > 0) {
            boolean[] checkedItems = new boolean[multipleChoiceItems.length];
            builder.setMultiChoiceItems(multipleChoiceItems, checkedItems, (dialog, which, isChecked) -> customDialogViewModel.onMultipleChoiceSelected(which, isChecked));
            isMultiple = true;
        }

        builder.setPositiveButton(positiveButtonText, (dialog, id) -> customDialogViewModel.onPositiveButtonClick())
                .setNegativeButton(negativeButtonText, (dialog, id) -> customDialogViewModel.onNegativeButtonClick());

        return builder.create();
    }
}

this is my Theme.xml:

<resources>
    <!-- Base application theme -->
    <style name="Theme.MyApp" parent="Theme.Material3.Light.NoActionBar">
        <!-- Primary brand color -->
        <item name="colorPrimary">@color/blue_gray_500</item>
        <item name="colorPrimaryVariant">@color/blue_gray_700</item>
        <item name="colorOnPrimary">@color/blue_gray_light</item>

        <!-- Secondary brand color -->
        <item name="colorSecondary">@color/soft_teal_200</item>
        <item name="colorSecondaryVariant">@color/soft_teal_700</item>
        <item name="colorOnSecondary">@color/grayish_black</item>

        <!-- Status bar color -->
        <item name="android:statusBarColor">@color/blue_gray_700</item>

        <!-- material components styling to dialogs -->
        <item name="alertDialogTheme">@style/CustomAlertDialogTheme</item>
    </style>

    <style name="CustomAlertDialogTheme" parent="ThemeOverlay.Material3.MaterialAlertDialog">
        <item name="colorPrimary">@color/blue_gray_500</item>
        <item name="colorAccent">@color/soft_teal_200</item>
        <item name="android:radioButtonStyle">@style/Widget.AppCompat.CompoundButton.RadioButton
        </item>
        <item name="android:listChoiceIndicatorSingle">@drawable/radio_button_checked</item>
        <item name="android:listChoiceIndicatorMultiple">@drawable/checkbox_selector</item>
    </style>
</resources>

Any insights on why the single or multiple choice buttons are not showing would be greatly appreciated. Thank you!

this is the debugger on the MaterialAlertDialogBuilder instance : builder

  1. i also tried to put only singleChoiceItems and only multiChoiceItems and it still not working.

  2. tried to write the MaterialAlertDialog within the fragment (not the DialogFragment) and it also didn't work

  3. several material design versions installations didn't workout (1.4.0,1.5.0,1.12.0 )

solved

delete the message and it start to show singleChioceButtons:

   MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireActivity());
        builder.setTitle(title);

        if (message != null) {
//            builder.setMessage(message);
        }

        // Log to check if the items are passed correctly
        Log.d("CustomDialogFragment", "Single choice items: " + Arrays.toString(singleChoiceItems));
        Log.d("CustomDialogFragment", "Multiple choice items: " + Arrays.toString(multipleChoiceItems));

        if (singleChoiceItems != null && singleChoiceItems.length > 0) {
            builder.setSingleChoiceItems(singleChoiceItems, -1, (dialog, which) -> customDialogViewModel.onSingleChoiceSelected(which));
            isSingle = true;
        }


        if (multipleChoiceItems != null && multipleChoiceItems.length > 0) {
            boolean[] checkedItems = new boolean[multipleChoiceItems.length];
            builder.setMultiChoiceItems(multipleChoiceItems, checkedItems, (dialog, which, isChecked) -> customDialogViewModel.onMultipleChoiceSelected(which, isChecked));
            isSingle = true;
        }


        builder.setPositiveButton(positiveButtonText, (dialog, id) -> customDialogViewModel.onPositiveButtonClick())
                .setNegativeButton(negativeButtonText, (dialog, id) -> customDialogViewModel.onNegativeButtonClick());

        return builder.create();
    }

0