I'm trying to create a more accessible, high-contrast mode for my app.
My current setup is as follows:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
<item name="colorPrimary">@color/primary_color</item>
<item name="colorPrimaryDark">@color/primary_color_dark</item>
<item name="colorAccent">@color/primary_color</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@color/background_fixed</item>
<item name="listPopupWindowStyle">@style/ListPopupWindow</item>
</style>
In the colors.xml file, is where I store all of the colours I use throughout the application.
All of the colours within colors.xml will need to slightly change if the user ticks "Accessibility mode". The solution I came up with was that I could create a copy of the original colors.xml file, but with the high contrast versions of the original colours, and when a user ticks the accessibility toggle, it will programmatically switch to the "accessibility_colors.xml" file (in which colours will be named identically to the original, but with different values)
I have done some research but can't find any information on this. Is this possible? If not - what is a viable solution for this?
CodePudding user response:
Color resources ids are generated during compiling time. So if you have a colors.xml file for default and another for, let's say a "night theme", you won't be able to do this programmatically on runtime.
One possible solution for this, would be to have this colors you need for "accessibility mode" to be created programmatically (hexadecimal code in code).
For example:
fun getPrimaryColor(isAccessibilityMode): Int {
return if(isAccessibilityMode) {
Color.parseColor("#bdbdbd");
} else {
Color.parseColor("#efefef"); // completely random colors here :)
}
}
You the would have to override the views default colors that you usually set in the theme style.