Add day/night theme spinner in SettingsActivity along with a very basic implementation of its behavior.

This commit is contained in:
Severiano Jaramillo 2018-10-30 13:14:33 -06:00
parent 30ae9c36bf
commit ad2b22a96c
5 changed files with 82 additions and 2 deletions

View file

@ -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()

View file

@ -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";
}

View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="32dp"
tools:context=".activities.SettingsActivity">
<Switch
android:id="@+id/switchNightMode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/night_mode"
android:textAppearance="@style/TextAppearance.Body1"
android:textColor="?android:textColorPrimary"/>
</LinearLayout>

View file

@ -8,4 +8,5 @@
<!-- Settings -->
<string name="title_settings">Settings</string>
<string name="night_mode">Night mode</string>
</resources>

View file

@ -1,6 +1,6 @@
<resources>
<!-- Base application theme. -->
<!-- Base application light theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
@ -12,6 +12,22 @@
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
<!-- Base application dark theme. -->
<style name="AppTheme.Dark" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Dark.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<!-- Text styles -->
<style name="TextAppearance.Body1" parent="TextAppearance.AppCompat.Body1" >
<item name="android:textSize">16sp</item>
</style>
<style name="TextAppearance.Body2" parent="Base.TextAppearance.AppCompat.Body2" />
</resources>