Add Dialog to change theme.

This commit is contained in:
2020-08-19 23:38:53 +02:00
parent 2c07e433e0
commit 26495f11a3
7 changed files with 60 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
package me.zobrist.tichucounter package me.zobrist.tichucounter
import android.app.AlertDialog
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
@@ -8,6 +9,7 @@ import androidx.core.widget.doOnTextChanged
import kotlinx.android.synthetic.main.content_main.* import kotlinx.android.synthetic.main.content_main.*
import android.text.InputType import android.text.InputType
import android.widget.ScrollView import android.widget.ScrollView
import androidx.appcompat.app.AppCompatDelegate
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
@@ -256,6 +258,10 @@ class MainActivity : AppCompatActivity() {
undoLastRound() undoLastRound()
true true
} }
R.id.action_theme -> {
chooseThemeDialog()
true
}
else -> super.onOptionsItemSelected(item) else -> super.onOptionsItemSelected(item)
} }
} }
@@ -317,4 +323,44 @@ class MainActivity : AppCompatActivity() {
submit.isEnabled = false submit.isEnabled = false
} }
private fun chooseThemeDialog() {
val builder = AlertDialog.Builder(this)
builder.setTitle(getString(R.string.choose_theme_text))
val styles = arrayOf("Light","Dark","System default")
var checkedItem = when(AppCompatDelegate.getDefaultNightMode()) {
AppCompatDelegate.MODE_NIGHT_NO -> 0
AppCompatDelegate.MODE_NIGHT_YES -> 1
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM -> 2
else -> -1
}
builder.setSingleChoiceItems(styles, checkedItem) { dialog, which ->
when (which) {
0 -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
delegate.applyDayNight()
dialog.dismiss()
}
1 -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
delegate.applyDayNight()
dialog.dismiss()
}
2 -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
delegate.applyDayNight()
dialog.dismiss()
}
}
}
val dialog = builder.create()
dialog.show()
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

View File

@@ -2,14 +2,19 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
tools:context="me.zobrist.tichucounter.MainActivity"> tools:context="me.zobrist.tichucounter.MainActivity">
<item
android:id="@+id/action_undo"
android:icon="@android:drawable/ic_menu_revert"
android:orderInCategory="5"
android:title="@string/undo" />
<item <item
android:id="@+id/action_clear" android:id="@+id/action_clear"
android:checkable="false" android:checkable="false"
android:orderInCategory="100" android:orderInCategory="10"
android:title="@string/clear" android:title="@string/clear"
app:showAsAction="never" /> app:showAsAction="never" />
<item <item
android:id="@+id/action_undo" android:id="@+id/action_theme"
android:icon="@android:drawable/ic_menu_revert" android:orderInCategory="15"
android:title="@string/undo" /> android:title="@string/choose_theme_text" />
</menu> </menu>

View File

@@ -2,4 +2,5 @@
<resources> <resources>
<string name="clear">Neues Spiel Starten</string> <string name="clear">Neues Spiel Starten</string>
<string name="undo">Letzte Runde Löschen</string> <string name="undo">Letzte Runde Löschen</string>
<string name="choose_theme_text">Theme auswählen</string>
</resources> </resources>

View File

@@ -2,4 +2,5 @@
<resources> <resources>
<string name="clear">Neus Spil Starte</string> <string name="clear">Neus Spil Starte</string>
<string name="undo">Letschti Rundi Lösche</string> <string name="undo">Letschti Rundi Lösche</string>
<string name="choose_theme_text">Usgsehe ändere</string>
</resources> </resources>

View File

@@ -6,4 +6,5 @@
<string name="team_b" translatable="false">Team B</string> <string name="team_b" translatable="false">Team B</string>
<string name="clear">Start New Game</string> <string name="clear">Start New Game</string>
<string name="undo">Undo Last Round</string> <string name="undo">Undo Last Round</string>
<string name="choose_theme_text">Choose theme</string>
</resources> </resources>

View File

@@ -1,10 +1,11 @@
<resources> <resources>
<!-- Base application theme. --> <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. --> <!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item> <item name="colorAccent">@color/colorAccent</item>
</style> </style>
<style name="AppTheme.NoActionBar"> <style name="AppTheme.NoActionBar">