简体   繁体   中英

How to change the List view text color the in Alert Dialog box using style in android?

I have used this line to add style to dialog box

AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle)

I am able to style the Title name however the text color in the list remains black color only.

    public class NotificationModeDialog extends DialogFragment {
        public ArrayList<String>  list = new ArrayList<>(); //initialized the array of list
        public  int i;
        final String notificationListItems[]={
    "CallAlert","address","name","Message","id","contact","ymail","Gmail"};//the data to be displayed in dialog box

        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final boolean[] notificationSelected={false,false,false,false,false,false,false,false};//initializing the starting state

//initializing the alertDialog here AlertDialogStyle is added for styling
            AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle)
                    .setTitle("Notification Mode")
                    .setMultiChoiceItems(notificationListItems, notificationSelected ,new DialogInterface.OnMultiChoiceClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int j, boolean isChecked) {
                            if(isChecked)
                            {
                                list.add(notificationListItems[j]); //add items 
                            }
                            else if(list.contains(notificationListItems[j]))
                            {
                                list.remove(notificationListItems[j]);//remove items
                            }

                        }
                    })
                             //when person click on "ok" this statement is called 
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int j) {

                        }
                    })

                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel(); //cancel the changes and turn of the dialog box
                        }
                    }).create();//this will create the dialog box
            dialog.show(); //show the dialog box
            return dialog; //return the dialog box to layout
            }
    }

This is styles.xml code.

  <!-- Styling the alert BOX -->
    <style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">

        <!-- Used for the buttons -->
        <item name="colorAccent">#e4e4e4</item>
        <!-- Used for the title and text -->
        <item name="android:textColorPrimary">#FFFFFF</item>
        <!-- Used for the background -->
        <item name="android:background">#64f24c49</item>

    </style>

Create a alert dialog style in styles.xml :

  <style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColor">@color/white</item>
    </style>

Then add the style to you current app theme style:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item> 

        <!-- Alert Dialog Style -->
        <item name="android:alertDialogStyle">@style/MyAlertDialogStyle</item>
    </style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM