I am doing a project where in i thought of providing "Localization" and "Dark Mode" for users.
For achieving Localization -> I have defined defined different string resources for different Locales under values-bn, values-es, values-hi, values-in, etc.
For changing Locale manually, i have used this code ->
Locale l = new Locale("our_locale_string");//It may be en,hi,.. etc.
Locale.setDefault(l);
Configuration config = new Configuration();
config.locale = l;
getResources().updateConfiguration(config,getResources().getDisplayMetrics());
I have used two styles.xml files for toggling between themes
For Light theme:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.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="cardViewStyle">@style/CardView</item>
<item name="android:windowBackground">@android:color/white</item>
</style>
For Dark theme:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="cardViewStyle">@style/CardView</item>
<item name="android:windowBackground">@android:color/black</item>
</style>
For changing theme to the overall app, i am using ->
if (mode) { //mode is a boolean variable to toggle b/n themes
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
I am able to achieve localization in light theme and also i am able to change to dark mode for the overall app. Here comes the problem, when i change the theme them respective strings are not used, instead default strings.xml(english generally) is used. How can i make it to use corresponding locale strings.xml .
My resource directory structure in "Project view"
res/values res/values-hi res/values-zh . . . res/values-es and for night mode, i have res/values-night/styles.xml
I have also tried creating nigth mode resource files, for each of the above different string files in values-xx folders like values-hi-night for example but that doesn't work.