Can't disable Night Mode in my application even with values-night
I had the same issue on my Xiaomi Redmi Note 7. I tried this code inside MainActivity.create()
:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
But it doesn't work on some of Xiaomi devices including mine, though it works on other phones.
So the only solution I've found is to add <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
to each of your AppTheme
in styles.xml
. Like this:
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>
It worked fine for my phone.
Dark Theme in android 9.0 changes my app layouts ugly
You can change App theme to any theme in the App Manifest file like,
<application
android:theme="@android:style/Theme.Holo"/>
<application
android:theme="@android:style/Theme.Holo.Light"/>
<application
android:theme="@android:style/Theme.Black"/>
<application
android:theme="@android:style/Theme.DeviceDefault"/>
Or you can customize theme in the styles.xml file and then Apply in Manifest
<style name="AppBaseTheme" parent="android:Theme.Holo.Light"/>
<style name="AppTheme" parent="AppBaseTheme"/>
Now in Manifest file
<application
android:theme="@style/AppBaseTheme"/>
You can also preview themes in the preview window in Android Studio.
The language changes automatically when I change the night mode
The problem that when you change the dark mode, the activity restarts with the default settings (default language), so you need to explicitly force the user's language.
But setting that explicitly every time you change the dark mode could be boilerplate, and this also could require a one more restart of the activity to apply the language.
Instead of that, you can use a customized ContextWrapper
where you can apply the preferred language at the very beginning of your app in the attachBaseContext()
activity callback by wrapping this customized ContextWrapper
:
The custom context wrapper:
public class MyContextWrapper extends ContextWrapper {
public MyContextWrapper(Context base) {
super(base);
}
public static ContextWrapper wrap(Context context, String language) {
Configuration config = context.getResources().getConfiguration();
Locale sysLocale;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
sysLocale = getSystemLocale(config);
} else {
sysLocale = getSystemLocaleLegacy(config);
}
if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setSystemLocale(config, locale);
} else {
setSystemLocaleLegacy(config, locale);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
context = context.createConfigurationContext(config);
} else {
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
return new MyContextWrapper(context);
}
public static Locale getSystemLocaleLegacy(Configuration config) {
return config.locale;
}
@TargetApi(Build.VERSION_CODES.N)
public static Locale getSystemLocale(Configuration config) {
return config.getLocales().get(0);
}
public static void setSystemLocaleLegacy(Configuration config, Locale locale) {
config.locale = locale;
}
@TargetApi(Build.VERSION_CODES.N)
public static void setSystemLocale(Configuration config, Locale locale) {
config.setLocale(locale);
}
}
Override attachBaseContext()
in the activity:
@Override
protected void attachBaseContext(Context context) {
sharedPreferences = context.getSharedPreferences("prefs", MODE_PRIVATE); // adjust the name of the SharedPreference to yours
String language = sharedPreferences.getString("Language", "en");
super.attachBaseContext(MyContextWrapper.wrap(context, language));
Locale locale = new Locale(language);
Resources resources = getBaseContext().getResources();
Configuration conf = resources.getConfiguration();
conf.locale = locale;
resources.updateConfiguration(conf, resources.getDisplayMetrics());
}
Android icons and radio buttons not following day/night mode
Well I played myself by using <item name="colorControlNormal">@color/white</item>
in my themes. After removing that the icon colors are correct now.
Related Topics
How to Create Topic in Fcm Notifications
How to Capture and Save an Image Using Custom Camera in Android
Broadcast Receiver Won't Receive Camera Event
Assign Width to Half Available Screen Width Declaratively
Live Audio Recording and Playing in Android and Thread & Callback Handling
Technical Details of Android Garbage Collector
How to Create a Multilingual Android Application
Job Scheduler Not Running on Android N
Android Pagerview Between Activities
How to Share Photo with Caption via Android Share Intent on Facebook
How to Declare Git Repository as Dependency in Android Gradle
Android Alert Dialog with One, Two, and Three Buttons
How to Fix "Failed to Sync Vcpu Reg" Error
How to Use Isineditmode() to See Layout with Custom View in the Editor
Remove Padding Around Action Bar Left Icon on Android 4.0+
How Does Bitmap Allocation Work on Oreo, and How to Investigate Their Memory
How to Extract the Text from the Selected Item on the Listview