Allow switching theme

This commit is contained in:
Sylvia van Os
2019-12-02 16:24:24 +01:00
parent 9b5a731d48
commit 8d3d9a21f7
8 changed files with 80 additions and 7 deletions

View File

@@ -5,6 +5,7 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.IntegerRes;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatDelegate;
import protect.card_locker.R;

View File

@@ -1,9 +1,13 @@
package protect.card_locker.preferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.util.Log;
import android.view.MenuItem;
import protect.card_locker.R;
@@ -44,12 +48,34 @@ public class SettingsActivity extends AppCompatActivity
public static class SettingsFragment extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
findPreference(getResources().getString(R.string.settings_key_theme)).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
if(o.toString().equals(getResources().getString(R.string.settings_key_light_theme)))
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
getActivity().recreate();
return true;
}
else if(o.toString().equals(getResources().getString(R.string.settings_key_dark_theme)))
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
getActivity().recreate();
return true;
}
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
getActivity().recreate();
return true;
}
});
}
}
}

View File

@@ -38,7 +38,7 @@
android:layout_width="match_parent"
android:background="@color/inputBorder" />
<TableRow
android:background="?android:attr/colorBackground"
android:background="@color/inputBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
@@ -90,7 +90,7 @@
android:layout_width="match_parent"
android:background="@color/inputBorder" />
<TableRow
android:background="?android:attr/colorBackground"
android:background="@color/inputBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
@@ -142,7 +142,7 @@
android:layout_width="match_parent"
android:background="@color/inputBorder" />
<TableRow
android:background="?android:attr/colorBackground"
android:background="@color/inputBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
@@ -214,7 +214,7 @@
android:layout_width="match_parent"
android:background="@color/inputBorder" />
<TableRow
android:background="?android:attr/colorBackground"
android:background="@color/inputBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
@@ -288,7 +288,7 @@
android:background="@color/inputBorder" />
<TableRow
android:id="@+id/cardIdTableRow"
android:background="?android:attr/colorBackground"
android:background="@color/inputBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
@@ -362,7 +362,8 @@
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:background="@color/inputBackground">
<LinearLayout android:orientation="horizontal"
android:padding="10.0dip"

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Palette generated by Material IO https://material.io/color/#!/?view.left=0&view.right=0&primary.color=3F51B5&secondary.color=FFC107 -->
<resources>
<color name="colorPrimary">#3f51b5</color>
<color name="colorPrimaryLight">#757de8</color>
<color name="colorPrimaryDark">#002984</color>
<color name="colorSecondary">#ffc107</color>
<color name="colorSecondaryLight">#fff350</color>
<color name="colorSecondaryDark">#c79100</color>
<color name="colorPrimaryText">#000000</color>
<color name="colorSecondaryText">#ffffff</color>
<color name="inputContrastBackground">#070707</color>
<color name="inputBackground">#000000</color>
<color name="inputBorder">#222222</color>
<color name="inputDividerBorder">#666666</color>
</resources>

View File

@@ -11,6 +11,7 @@
<color name="colorSecondaryText">#000000</color>
<color name="inputContrastBackground">#F8F8F8</color>
<color name="inputBackground">#FFFFFF</color>
<color name="inputBorder">#DDDDDD</color>
<color name="inputDividerBorder">#AAAAAA</color>
</resources>

View File

@@ -1,5 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="theme_values">
<item>@string/settings_key_system_theme</item>
<item>@string/settings_key_light_theme</item>
<item>@string/settings_key_dark_theme</item>
</string-array>
<string-array name="theme_value_strings">
<item>@string/settings_system_theme</item>
<item>@string/settings_light_theme</item>
<item>@string/settings_dark_theme</item>
</string-array>
<array name="letter_tile_colors">
<item>#f16364</item>
<item>#f58559</item>

View File

@@ -109,6 +109,14 @@
<string name="settings">Settings</string>
<string name="settings_category_title_ui">User interface</string>
<string name="settings_theme">Theme</string>
<string name="settings_key_theme" translatable="false">pref_theme</string>
<string name="settings_system_theme">System theme</string>
<string name="settings_key_system_theme" translatable="false">system</string>
<string name="settings_light_theme">Light theme</string>
<string name="settings_key_light_theme" translatable="false">light</string>
<string name="settings_dark_theme">Dark theme</string>
<string name="settings_key_dark_theme" translatable="false">dark</string>
<string name="settings_card_title_list_font_size">Card title list font size</string>
<string name="settings_key_card_title_list_font_size" translatable="false">pref_card_title_list_font_size_sp</string>
<string name="settings_card_note_list_font_size">Card note list font size</string>

View File

@@ -6,6 +6,13 @@
<PreferenceCategory
android:title="@string/settings_category_title_ui">
<ListPreference
android:key="@string/settings_key_theme"
android:title="@string/settings_theme"
android:defaultValue="@string/settings_key_system_theme"
android:entries="@array/theme_value_strings"
android:entryValues="@array/theme_values"/>
<com.vanniktech.vntnumberpickerpreference.VNTNumberPickerPreference
android:key="@string/settings_key_card_title_list_font_size"
android:title="@string/settings_card_title_list_font_size"