4

I have a simple alert dialog with single choice items

val alertDialog = AlertDialog.Builder(this)
alertDialog.setTitle("My Title")
val array = arrayOf("aa", "bb", "cc")
alertDialog.setSingleChoiceItems(array, 0) { _, selectedItem ->
}
alertDialog.setNegativeButton("Cancel") { _, _ ->
}
alertDialog.create()
alertDialog.show()

I've added to my theme the custom style of alert dialog

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

     .........
     <item name="android:timePickerDialogTheme">@style/DefaultAlertDialogTheme</item>
     <item name="android:datePickerDialogTheme">@style/DefaultAlertDialogTheme</item>
     <item name="android:alertDialogTheme">@style/DefaultAlertDialogTheme</item>
</style>

And here's my style

<style name="DefaultAlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:colorAccent">@color/greenButtonBackgroundColor</item>
    <item name="android:textColorSecondary">@color/greenButtonBackgroundColor</item>
    <!--title-->
    <item name="android:textColor">@color/toolbarTitleTextColor</item>
</style>

When I run the app I see this alert

enter image description here

Is there a way to change the color of the text near the radio button without creating the custom layout?

Vitalii
  • 10,091
  • 18
  • 83
  • 151

4 Answers4

7

Style to colorize radio button items

<style name="DefaultAlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
    <!--        Title Color-->
    <item name="android:textColorPrimary">@color/black</item>
    <!--        Radio button unchecked-->
    <item name="android:colorControlNormal">@color/medium_gray</item>
    <!--        Radio button checked-->
    <item name="android:colorControlActivated">@color/warmPink</item>
    <!--        List item on click highlight color-->
    <item name="android:colorControlHighlight">@color/transparent</item>
</style>
Dmytro Puzak
  • 221
  • 3
  • 5
  • 1
    It works... If you want to change also radiobutton text color add " @color/color_detect_button_color" – Ucdemir Mar 22 '21 at 06:21
2

you can do it in style

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

then in your code do this

val alertDialog = AlertDialog.Builder(this, R.style.MyDialogTheme)
UnBanned
  • 121
  • 11
1

Try this

 val array = arrayOf(Html.fromHtml("<font color='#00FF00'>aa</font>"),
                Html.fromHtml("<font color='#00FF00'>bb</font>"),
                Html.fromHtml("<font color='#00FF00'>cc</font>"))

You can see more details here

E I
  • 501
  • 4
  • 13
0
Add into Style to change the color of radio button items

<style name="DefaultAlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:colorControlNormal">@color/gray</item
    <item name="android:colorControlActivated">@color/blue</item>
    <item name="android:colorControlHighlight">@color/light_gray</item>
</style>

If you need to change radiobutton text color add this in your styles" 

    <item name="textColorAlertDialogListItem">@color/color_white</item>"