I want to find a way to set the background color of an ImageButton to a color that I defined in my themes and which changes with day or night mode.
Is this possible?
Here is the color that I want (colorPrimary) in the themes file:
<item name="colorPrimary">@color/blue_900</item>
Thank you very much.
Bapt
According to Material Design guidelines (see here),
styles.xml
, inherit from a DayNight
theme. <style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
...
...
</style>
colors.xml
file under the values-night
directory.values\colors.xml
, <color name="primary_color">{your-light-theme-color-here}</color>
In values-night\colors.xml
<color name="primary_color">{your-dark-theme-color-here}</color>
styles.xml
, <style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">@color/primary_color</item>
...
</style>
AndroidManifest.xml
<application
...
android:theme="@style/Theme.MyApp">
</application>
I feel this is the most guideline-appropriate and efficient way.