Search code examples
javaandroiddialogandroid-alertdialog

Android - Set TextColor ListItem on an AlertDialog with list programatically


I have an AlertDialog where a user must choose between several options. The default color scheme for this is white text on a darkgrey background. In the App users can configure their own color which are suppose to match with white. Changing the background to white isn't the issue, but I cannot set the textcolor of the items.

I was looking for something like dialog.getItems.setTextColor([usercolor]);, but I haven't found a solution for it. Below my code for building the dialog.

    tvRekening.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(a);
                builder.setTitle("Title")
                        .setItems(values, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {
                                // The 'which' argument contains the index position
                                // of the selected item
                                setRekeningView(which);
                            }
                        });
                AlertDialog dialog = builder.create();
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(a, R.color.white)));
                dialog.show();
            }
        });

To further illustrate the issue:

The default color scheme: https://i.sstatic.net/nMKAA.png

Changing the background color to white makes the text unreadable (because it's still white). https://i.sstatic.net/PEABc.png

EDIT: I understand I can create a custom adapter for this. But creating an Adapter only for editing the text color of the items seems a bit of an overkill. If someone knows a solution without going through the trouble of creating an adapter, it would be appreciated. If not i'll close the question in a while.


Solution

  • you need to create custom dialog like this:

    Dialog d =new Dialog(context);
    d.setContentView(R.layout.dialog); 
    ListView l=((ListView )d.findViewById(R.id.list));
    

    see this link Android - Executing a custom listview in a custom dialog properly