From ad2b22a96cdfe63953a0017e2ab41a2278fb9afe Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Tue, 30 Oct 2018 13:14:33 -0600 Subject: [PATCH] Add day/night theme spinner in SettingsActivity along with a very basic implementation of its behavior. --- .../activities/SettingsActivity.kt | 33 +++++++++++++++++++ .../bitsybitshareswallet/utils/Constants.java | 8 +++++ app/src/main/res/layout/activity_settings.xml | 22 +++++++++++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/values/styles.xml | 20 +++++++++-- 5 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.java create mode 100644 app/src/main/res/layout/activity_settings.xml diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/SettingsActivity.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/SettingsActivity.kt index 550c620..83d5d21 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/SettingsActivity.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/SettingsActivity.kt @@ -1,8 +1,12 @@ package cy.agorise.bitsybitshareswallet.activities import android.os.Bundle +import android.preference.PreferenceManager import android.view.MenuItem import android.support.v7.app.AppCompatActivity +import cy.agorise.bitsybitshareswallet.R +import cy.agorise.bitsybitshareswallet.utils.Constants +import kotlinx.android.synthetic.main.activity_settings.* /** * A simple activity for the user to select his preferences @@ -11,7 +15,20 @@ class SettingsActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + + // TODO move the below to a BaseActivity to apply it to all activities + // Sets the theme to night mode if it has been selected by the user + if (PreferenceManager.getDefaultSharedPreferences(this) + .getBoolean(Constants.KEY_NIGHT_MODE_ACTIVATED, false) + ) { + setTheme(R.style.AppTheme_Dark) + } + + setContentView(R.layout.activity_settings) + setupActionBar() + + initNightModeSwitch() } /** @@ -21,6 +38,22 @@ class SettingsActivity : AppCompatActivity() { supportActionBar?.setDisplayHomeAsUpEnabled(true) } + private fun initNightModeSwitch() { + val nightModeOn = PreferenceManager.getDefaultSharedPreferences(this) + .getBoolean(Constants.KEY_NIGHT_MODE_ACTIVATED, false) + + switchNightMode.isChecked = nightModeOn + + switchNightMode.setOnCheckedChangeListener { buttonView, isChecked -> + + PreferenceManager.getDefaultSharedPreferences(buttonView.context).edit() + .putBoolean(Constants.KEY_NIGHT_MODE_ACTIVATED, isChecked).apply() + + // Recreates the activity to apply the selected theme + this.recreate() + } + } + override fun onOptionsItemSelected(item: MenuItem): Boolean { if (item.itemId == android.R.id.home) { onBackPressed() diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.java b/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.java new file mode 100644 index 0000000..b5cbe19 --- /dev/null +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.java @@ -0,0 +1,8 @@ +package cy.agorise.bitsybitshareswallet.utils; + +public class Constants { + /** + * Key used to store the night mode setting into the shared preferences + */ + public static final String KEY_NIGHT_MODE_ACTIVATED = "key_night_mode_activated"; +} diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml new file mode 100644 index 0000000..8b6045a --- /dev/null +++ b/app/src/main/res/layout/activity_settings.xml @@ -0,0 +1,22 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 52379d4..7c1d12d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -8,4 +8,5 @@ Settings + Night mode diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 16dbab3..f95b2ee 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,6 +1,6 @@ - + + + + + +