From d2328d4b9a33469f9adc3fcbe9fab9a28a7e1da8 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sat, 5 Nov 2022 10:13:35 +0100 Subject: [PATCH] Automatic day night theme in settings activity There seems to be no "DayNight" theme compatible with older version of android outside of the androidx library. Using 'Theme.DeviceDefault' which is a dark theme, even if it doesn't sounds like. Detect if a light theme should be used at activity creation. --- AndroidManifest.xml | 2 +- srcs/juloo.keyboard2/SettingsActivity.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 4e29f0c..2489589 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -8,7 +8,7 @@ - + diff --git a/srcs/juloo.keyboard2/SettingsActivity.java b/srcs/juloo.keyboard2/SettingsActivity.java index 9ec82c5..98cd590 100644 --- a/srcs/juloo.keyboard2/SettingsActivity.java +++ b/srcs/juloo.keyboard2/SettingsActivity.java @@ -1,5 +1,7 @@ package juloo.keyboard2; +import android.content.res.Configuration; +import android.os.Build; import android.os.Bundle; import android.preference.PreferenceActivity; @@ -8,7 +10,20 @@ public class SettingsActivity extends PreferenceActivity @Override public void onCreate(Bundle savedInstanceState) { + detectSystemTheme(); super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings); } + + /** The default theme is [Theme.DeviceDefault], which is dark. Detect if the + system is using light theme. */ + void detectSystemTheme() + { + if (Build.VERSION.SDK_INT >= 14) + { + int ui_mode = getResources().getConfiguration().uiMode; + if ((ui_mode & Configuration.UI_MODE_NIGHT_NO) != 0) + setTheme(android.R.style.Theme_DeviceDefault_Light); + } + } }