Search code examples
androidkotlincolorsandroid-alertdialog

Change text color of dialog option items


I have this dialog in which the options appear with black text, and I would like to make it white

@SuppressLint("CutPasteId")
private fun showPageSelectionDialog() {
    val options = arrayOf("A4", "A5") // User options

    val builder = AlertDialog.Builder(this, R.style.MyAlertDialogStyle)
    builder.setTitle("Select PDF size")
    .setItems(options) { _, which ->
        val selectedPageSize = if (which == 1) PageSize.A5 else PageSize.A4
        pdf(name.text.toString().trim(), selectedPageSize)
    }
    .setNegativeButton("Cancel") { dialog, _ ->
        dialog.dismiss()
    }

    builder.create().show()
}

Since I use more dialogs in my app I created a custom style:

<!-- DIALOGUE -->
    <style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:windowBackground">@drawable/dialog_background</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:textColorSecondary">@color/blue</item>
        <item name="android:textAppearance">@color/white</item>
    </style>

Also previously used:

        <item name="android:textColor">@color/white</item>

And it didn't work.


Solution

  • As Jorgesys answered in this question: Cambiar color y propiedades de texto en un AlertDialog, what solved the problem for me was adding this line to my options variable:

    val options = arrayOf(Html.fromHtml("<font color='#FFFFFF'>A4</font>"),
                            Html.fromHtml("<font color='#FFFFFF'>A5</font>"))
    

    Although it is an obsolete method, it is the only one I have found that works for me.