Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of single choice alert dialog?

Tags:

java

android

How do I change the colour of a radio button? The image below shows that the radio buttons are blue by default but I would like to change this.

enter image description here

My code is:

 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);
            alertDialogBuilder.setCustomTitle(promptsView);
            alertDialogBuilder.setItems(frequencyName, null);
            // alertDialogBuilder.setTitle("SELECT FREQUENCY");


            alertDialogBuilder.setSingleChoiceItems(frequencyName, -1, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    selectedFre = frequencyName[i];
                }
            });

            alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {

                                    txtFrequency.setText(selectedFre);
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            });


            AlertDialog alertDialog = alertDialogBuilder.create();


            alertDialog.show();
like image 697
Vikash Kumar Avatar asked Jan 04 '23 23:01

Vikash Kumar


1 Answers

Create corresponding style for AlertDialog

<style name="MaterialThemeDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/action_bar_background</item>
</style>

Create AlertDialog.Builder using that style like below example.

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            getActivity(),
            R.style.MaterialThemeDialog);
    alertDialogBuilder.setTitle(R.string.image_resolution);
    alertDialogBuilder.setSingleChoiceItems(R.array.quality_labels, getPosition(), this);

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
like image 59
Priyank Patel Avatar answered Jan 26 '23 10:01

Priyank Patel