How to specify android custom color resources: day/night/high contrast
Asked Answered
T

1

6

Android supports Day/Night mode with

../values/colors.xml and

../values-night/colors.xml.

If I wanted to add another color folder, say for high contrast colors

(../values-highconst/colors.xml), is there a way to add extra color folders and make it pick the colors in that extra folder based on meeting a certain condition?

I thought of doing it with standard_theme.xml, high_contrast_theme.xml, and when we try to also add day/night mode to each theme, it gets harder to manage and maintain with multiple developers working on the project. How do we achieve that?

Transaction answered 9/1, 2019 at 5:1 Comment(0)
S
3

you have to define attrs.xml like below ..res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
  <resources>
    <declare-styleable name="tm">
     <attr name="background_color" format="color" />
     <attr name="card_background" format="color" />
     <attr name="text_color" format="color" />
     <attr name="tint_color" format="color" />
     <attr name="button_color" format="color" />
     <attr name="button_text_color" format="color" />
     <attr name="icon_color" format="color" />
     <attr name="toolbar_color" format="color" />
     <attr name="toolbar_content_color" format="color" />
     <attr name="edit_text_background" format="color" />
     <attr name="edit_text_color" format="color" />
     <attr name="edit_text_hint" format="color" />
 </declare-styleable>
</resources>

and then define styles in ../res/values/styles like below

 <!-- Base application theme. -->
<!-- Base light/day theme. -->
<style name="darkTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="background_color">#fcffe3</item>
    <item name="card_background">#2d2e30</item>
    <item name="text_color">#999797</item>
    <item name="toolbar_color">@color/black</item>
    <item name="toolbar_content_color">@color/yellowText</item>
    <item name="tint_color">@color/colorAccent</item>
    <item name="button_color">@color/colorPrimary</item>
    <item name="model_item_color">@color/colorPrimary</item>
    <item name="icon_color">@color/colorAccent</item>
    <item name="text_icon_dr">@color/black</item>
    <item name="button_text_color">@color/black</item>
    <item name="hint_color">#6e6f70</item>
    <item name="btn_color">@color/yellowButton</item>
    <item name="edit_text_background">@color/white</item>
    <item name="edit_text_color">@color/black</item>
    <item name="edit_text_hint">@color/brown_400</item>
    <item name="text_bg_white">@color/black</item>
</style>

<!-- Base dark/night theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">#9c2917</item>
    <item name="background_color">@color/colorPrimary</item>
    <item name="card_background">#d3d6d7</item>
    <item name="text_color">@color/white</item>
    <item name="toolbar_color">@color/white</item>
    <item name="toolbar_content_color">@color/yellowText</item>
    <item name="tint_color">#d1d100</item>
    <item name="button_color">@color/colorPrimary</item>
    <item name="model_item_color">@color/colorPrimary</item>
    <item name="icon_color">#331611</item>
    <item name="text_icon_dr">@color/colorAccent</item>
    <item name="button_text_color">@color/black</item>
    <item name="hint_color">#9ef9f9f9</item>
    <item name="edit_text_background">@color/white</item>
    <item name="edit_text_color">@color/black</item>
    <item name="edit_text_hint">@color/brown_400</item>
    <item name="text_bg_white">@color/black</item>
    <item name="btn_color">@color/yellowButton</item>
</style>

it's work for me for multi theme Hope to be useful to you

Scandinavia answered 9/1, 2019 at 5:41 Comment(2)
you could achieve what you mentioned without the attr.xml using the pattern below. <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"> <item name="colorPrimary">@color/Primary</color> </style> and then in values and values-night colors specify a different color like below: ../values/colors.xml -> <color name="colorPrimary">#FFFFFF</color> ../values-night/colors.xml -> <color name="colorPrimary">#00000</color> And in your code use AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES) and changing the MODE as your need. I wanted ../values-highcontrst/colors.xml.Transaction
I wanted a folder like ../values-highcontrst/colors.xml And I want to specify the colorPrimary in this folder like I did for day and night colorsTransaction

© 2022 - 2024 — McMap. All rights reserved.