🐷
Android AlertDialogのテーマ設定
いつも忘れるのでメモ.
Composableじゃなくて旧AlertDialogの方.
androidのsytleってどれがどれに対応しているのか規則性が読み取れず、全く覚えられない問題..
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="alertDialogTheme">@style/DayNightAlertDialog</item>
</style>
<style name="DayNightAlertDialog" parent="Theme.AppCompat.DayNight.Dialog.Alert">
<!-- background color -->
<item name="android:background">@color/background</item>
<!-- title color -->
<item name="android:textColor">@color/accent_1000</item>
<!-- description color -->
<item name="android:textColorPrimary">@color/accent_1000</item>
<!-- buttons -->
<item name="buttonBarPositiveButtonStyle">@style/DayNightAlertDialog.PositiveButton</item>
<item name="buttonBarNegativeButtonStyle">@style/DayNightAlertDialog.NegativeButton</item>
<item name="buttonBarNeutralButtonStyle">@style/DayNightAlertDialog.NeutralButton</item>
</style>
<style name="DayNightAlertDialog.PositiveButton" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">@color/tint_color</item>
</style>
<style name="DayNightAlertDialog.NegativeButton" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">@color/tint_color</item>
</style>
<style name="DayNightAlertDialog.NeutralButton" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">@color/accent_700</item>
</style>
</resources>
これをAndroidManifest.xmlでtheme設定して、
<application ...>
<activity
android:name=".AppActivity"
android:exported="false"
android:theme="@style/Theme.MyApp" />
...
title, mesage, ok, ng, cancel, bgに色が正常につく
AlertDialog.Builder(this)
.setTitle("title")
.setMessage("message")
.setPositiveButton("OK") { dialog, _ ->
dialog.dismiss()
}
.setNegativeButton("NG") { dialog, _ ->
dialog.dismiss()
}
.setNeutralButton("Cancel") { dialog, _ ->
dialog.dismiss()
}
.show()
Discussion