Search code examples
androidkotlinandroid-alertdialogandroid-themematerial-components-android

Alert Dialog doesn't adapt to Dark Mode and non-dark mode


Why doesn't my Alert Dialog adapt to my phone's Dark Mode? When Dark Mode is activated in my phone, the two buttons' text is barely visible, while the title and message colors are inverted. why aren't buttons color inverted as well? The icon also doesn't appear well in non-dark mode. how to fix all of this?

enter image description here enter image description here

  val builder = AlertDialog.Builder(this)
  builder.setTitle(getString(R.string.title))
  builder.setMessage(getString(R.string.message))

  builder.setPositiveButton(getString(R.string.positive)) { dialog, which ->
        
    Toast.makeText(applicationContext, getString(R.string.pos_toast), Toast.LENGTH_LONG).show()
  }

  builder.setNegativeButton(getString(R.string.negative)) { dialog, which ->

    Toast.makeText(applicationContext, getString(R.string.negative_toast), Toast.LENGTH_LONG).show()
  }

  builder.setIcon(android.R.drawable.ic_dialog_alert)
  builder.show()

my stiles.xml:

<resources>

  <!-- Base application theme. -->
  <style name="MyTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

  <style name="generalnotitle" parent="MyTheme">
    <item name="android:windowNoTitle">true</item>
  </style>

</resources>

Solution

  • Since you are using a Material Components Theme just use the MaterialAlertDialogBuilder instead of AlertDialog.Builder:

        MaterialAlertDialogBuilder(this)
                .setTitle("Title")
                .setMessage("Message")
                .setNegativeButton("CANCEL") { dialog, which ->
                    // Respond to neutral button press
                }
                .setPositiveButton("OK") { dialog, which ->
                    // Respond to positive button press
                }
                .show()
    

    The default text color of the buttons are based on this selector:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_checkable="true" android:state_checked="true" android:state_enabled="true"/>
      <item android:alpha="0.60" android:color="?attr/colorOnSurface" android:state_checkable="true" android:state_checked="false" android:state_enabled="true"/>
      <item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_enabled="true"/>
      <item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
    </selector>
    

    enter image description here

    Just check the colorPrimary defined in your app theme for the dark mode.

    If you want to change the text color of the buttons you can override the colorPrimary using:

    MaterialAlertDialogBuilder(this, R.style.Theme_MyApp_Dialog_Alert)
    

    with:

    <style name="Theme.MyApp.Dialog.Alert" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    
        <!-- Text Color for Buttons -->
        <item name="colorPrimary">@color/...</item>
    
    </style>
    

    enter image description here