Update. Add language setting.
This commit is contained in:
@@ -4,7 +4,6 @@ apply plugin: 'kotlin-android-extensions'
|
||||
|
||||
android {
|
||||
compileSdkVersion 30
|
||||
buildToolsVersion "30.0.2"
|
||||
|
||||
defaultConfig {
|
||||
applicationId "me.zobrist.tichucounter"
|
||||
|
||||
BIN
app/release/app-release.aab
Normal file
BIN
app/release/app-release.aab
Normal file
Binary file not shown.
@@ -13,8 +13,8 @@
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:windowSoftInputMode="adjustPan"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme.NoActionBar">
|
||||
android:theme="@style/AppTheme.NoActionBar"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
|
||||
34
app/src/main/java/me/zobrist/tichucounter/ContextUtils.kt
Normal file
34
app/src/main/java/me/zobrist/tichucounter/ContextUtils.kt
Normal file
@@ -0,0 +1,34 @@
|
||||
package me.zobrist.tichucounter
|
||||
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.content.res.Configuration
|
||||
import android.content.res.Resources
|
||||
import android.os.Build
|
||||
import android.os.LocaleList
|
||||
import java.util.*
|
||||
|
||||
class ContextUtils(base: Context) : ContextWrapper(base) {
|
||||
|
||||
companion object {
|
||||
|
||||
fun updateLocale(c: Context, localeToSwitchTo: Locale): ContextWrapper {
|
||||
var context = c
|
||||
val resources: Resources = context.resources
|
||||
val configuration: Configuration = resources.configuration
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
val localeList = LocaleList(localeToSwitchTo)
|
||||
LocaleList.setDefault(localeList)
|
||||
configuration.setLocales(localeList)
|
||||
} else {
|
||||
configuration.locale = localeToSwitchTo
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
||||
context = context.createConfigurationContext(configuration)
|
||||
} else {
|
||||
resources.updateConfiguration(configuration, resources.displayMetrics)
|
||||
}
|
||||
return ContextUtils(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package me.zobrist.tichucounter
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.text.InputType
|
||||
import android.view.Menu
|
||||
@@ -14,6 +16,7 @@ import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.core.widget.doOnTextChanged
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.android.synthetic.main.content_main.*
|
||||
import java.util.*
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
@@ -21,6 +24,15 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var history: History
|
||||
private var currentRound = Round()
|
||||
private var systemLocale = Locale.getDefault()
|
||||
|
||||
override fun attachBaseContext(newBase: Context) {
|
||||
// get chosen language from shread preference
|
||||
val localeString = newBase.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("Language", systemLocale.toString())
|
||||
val localeToSwitchTo = Locale(localeString)
|
||||
val localeUpdatedContext: ContextWrapper = ContextUtils.updateLocale(newBase, localeToSwitchTo)
|
||||
super.attachBaseContext(localeUpdatedContext)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -36,26 +48,32 @@ class MainActivity : AppCompatActivity() {
|
||||
.getBoolean("Screen_On", false)
|
||||
)
|
||||
|
||||
|
||||
val json = this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("history", "{\"scores\":[]}")
|
||||
history = Gson().fromJson(json, History::class.java)
|
||||
nameTeamA.setText(this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("nameTeamA", "TeamA"))
|
||||
nameTeamB.setText(this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("nameTeamB", "TeamB"))
|
||||
updateView()
|
||||
|
||||
this.setListenes()
|
||||
|
||||
inputTeamA.setOnFocusChangeListener { view, b ->
|
||||
}
|
||||
|
||||
private fun setListenes()
|
||||
{
|
||||
inputTeamA.setOnFocusChangeListener { _, b ->
|
||||
if (b) {
|
||||
hideKeyboard()
|
||||
}
|
||||
}
|
||||
|
||||
inputTeamB.setOnFocusChangeListener { view, b ->
|
||||
inputTeamB.setOnFocusChangeListener { _, b ->
|
||||
if (b) {
|
||||
hideKeyboard()
|
||||
}
|
||||
}
|
||||
|
||||
inputTeamA.doOnTextChanged { text, start, count, after ->
|
||||
inputTeamA.doOnTextChanged { text, _, _, _ ->
|
||||
if (inputTeamA.isFocused) {
|
||||
if (inputTeamA.text.isNotEmpty()) {
|
||||
if (updateOnChange) {
|
||||
@@ -82,9 +100,7 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
inputTeamB.doOnTextChanged { text, start, count, after ->
|
||||
inputTeamB.doOnTextChanged { text, _, _, _ ->
|
||||
if (inputTeamB.isFocused) {
|
||||
if (inputTeamB.text.isNotEmpty()) {
|
||||
if (updateOnChange) {
|
||||
@@ -323,11 +339,11 @@ class MainActivity : AppCompatActivity() {
|
||||
builder.setMessage(getString(R.string.confirmClear))
|
||||
.setTitle(R.string.clear)
|
||||
.setCancelable(false)
|
||||
.setPositiveButton(getString(R.string.yes)) { dialog, id ->
|
||||
.setPositiveButton(getString(R.string.yes)) { dialog, _ ->
|
||||
dialog.dismiss()
|
||||
clearAll()
|
||||
}
|
||||
.setNegativeButton(getString(R.string.no)) { dialog, id ->
|
||||
.setNegativeButton(getString(R.string.no)) { dialog, _ ->
|
||||
dialog.cancel()
|
||||
}
|
||||
|
||||
@@ -342,6 +358,10 @@ class MainActivity : AppCompatActivity() {
|
||||
chooseThemeDialog()
|
||||
true
|
||||
}
|
||||
R.id.action_language -> {
|
||||
chooseLanguageDialog()
|
||||
true
|
||||
}
|
||||
R.id.action_screenOn -> {
|
||||
item.isChecked = !item.isChecked
|
||||
keepScreenOn(item.isChecked)
|
||||
@@ -409,7 +429,7 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
val builder = AlertDialog.Builder(this)
|
||||
builder.setTitle(getString(R.string.choose_theme_text))
|
||||
val styles = arrayOf("Light", "Dark", "System default")
|
||||
val styles = arrayOf(getString(R.string.light), getString(R.string.dark), getString(R.string.android_default_text))
|
||||
|
||||
val checkedItem =
|
||||
this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getInt("Theme", 2)
|
||||
@@ -431,6 +451,42 @@ class MainActivity : AppCompatActivity() {
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
private fun chooseLanguageDialog() {
|
||||
|
||||
val builder = AlertDialog.Builder(this)
|
||||
builder.setTitle(getString(R.string.choose_language_text))
|
||||
|
||||
val languages_map = mapOf(
|
||||
getString(R.string.android_default_text) to systemLocale.toString(),
|
||||
getString(R.string.english) to "en",
|
||||
getString(R.string.german) to "de")
|
||||
|
||||
val languages_display_keys = languages_map.keys.toTypedArray()
|
||||
val languages_display_values = languages_map.values.toTypedArray()
|
||||
|
||||
|
||||
val checkedItem = this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("Language", R.string.android_default_text.toString())
|
||||
val checkedItemIndex = languages_display_values.indexOf(checkedItem)
|
||||
|
||||
val prefs = this.getSharedPreferences("Settings", Context.MODE_PRIVATE).edit()
|
||||
|
||||
|
||||
builder.setSingleChoiceItems(languages_display_keys, checkedItemIndex) { dialog, which ->
|
||||
|
||||
val temp = languages_map[languages_display_keys[which]]
|
||||
prefs.putString("Language", temp)
|
||||
prefs.apply()
|
||||
|
||||
startActivity(Intent(this, MainActivity::class.java))
|
||||
finish()
|
||||
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
val dialog = builder.create()
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
private fun updateTheme(which: Int) {
|
||||
when (which) {
|
||||
0 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
||||
|
||||
@@ -59,7 +59,8 @@
|
||||
android:text="0"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold" />
|
||||
android:textStyle="bold"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/scoreB"
|
||||
@@ -70,7 +71,8 @@
|
||||
android:text="0"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold" />
|
||||
android:textStyle="bold"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
@@ -136,7 +138,8 @@
|
||||
android:gravity="center"
|
||||
android:hint="0"
|
||||
android:importantForAutofill="no"
|
||||
android:inputType="numberSigned" />
|
||||
android:inputType="numberSigned"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/inputTeamB"
|
||||
@@ -147,7 +150,8 @@
|
||||
android:gravity="center"
|
||||
android:hint="0"
|
||||
android:importantForAutofill="no"
|
||||
android:inputType="numberSigned" />
|
||||
android:inputType="numberSigned"
|
||||
tools:ignore="HardcodedText" />
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
@@ -164,7 +168,8 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="1" />
|
||||
android:text="1"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button2"
|
||||
@@ -172,7 +177,8 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="2" />
|
||||
android:text="2"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button3"
|
||||
@@ -180,7 +186,8 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="3" />
|
||||
android:text="3"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/buttonAdd100"
|
||||
@@ -188,7 +195,8 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="+100" />
|
||||
android:text="+100"
|
||||
tools:ignore="HardcodedText" />
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
@@ -205,7 +213,8 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="4" />
|
||||
android:text="4"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button5"
|
||||
@@ -213,7 +222,8 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="5" />
|
||||
android:text="5"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button6"
|
||||
@@ -221,7 +231,8 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="6" />
|
||||
android:text="6"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/buttonSub100"
|
||||
@@ -229,7 +240,8 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="-100" />
|
||||
android:text="-100"
|
||||
tools:ignore="HardcodedText" />
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
@@ -246,7 +258,8 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="7" />
|
||||
android:text="7"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button8"
|
||||
@@ -254,7 +267,8 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="8" />
|
||||
android:text="8"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button9"
|
||||
@@ -262,7 +276,8 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="9" />
|
||||
android:text="9"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/buttonBack"
|
||||
@@ -275,7 +290,7 @@
|
||||
android:paddingBottom="15dp"
|
||||
android:scaleType="fitCenter"
|
||||
app:srcCompat="@drawable/back"
|
||||
android:contentDescription="TODO" />
|
||||
android:contentDescription="@string/back" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@@ -17,6 +17,10 @@
|
||||
android:id="@+id/action_theme"
|
||||
android:orderInCategory="15"
|
||||
android:title="@string/choose_theme_text" />
|
||||
<item
|
||||
android:id="@+id/action_language"
|
||||
android:orderInCategory="16"
|
||||
android:title="@string/choose_language_text" />
|
||||
<item
|
||||
android:id="@+id/action_screenOn"
|
||||
android:checkable="true"
|
||||
|
||||
@@ -7,4 +7,11 @@
|
||||
<string name="confirmClear">Möchten Sie das laufende Spiel wirklich löschen?</string>
|
||||
<string name="yes">Ja</string>
|
||||
<string name="no">Nein</string>
|
||||
<string name="back">Zurück</string>
|
||||
<string name="choose_language_text">Sprache wählen</string>
|
||||
<string name="android_default_text">Android Standard</string>
|
||||
<string name="english">Englisch</string>
|
||||
<string name="german">Detusch</string>
|
||||
<string name="light">Hell</string>
|
||||
<string name="dark">Dunkel</string>
|
||||
</resources>
|
||||
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="clear">Neus Spil starte</string>
|
||||
<string name="undo">Letschti Rundi lösche</string>
|
||||
<string name="choose_theme_text">Usgsehe ändere</string>
|
||||
<string name="keep_screen_on">Bildschirm igschalted la</string>
|
||||
<string name="confirmClear">Wosch ds loufende Spil würklech lösche?</string>
|
||||
<string name="yes">Ja</string>
|
||||
<string name="no">Nei</string>
|
||||
</resources>
|
||||
@@ -1,9 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name" translatable="false">Tichu Counter</string>
|
||||
<!-- Strings used for fragments for navigation -->
|
||||
|
||||
<string name="team_a" translatable="false">Team A</string>
|
||||
<string name="team_b" translatable="false">Team B</string>
|
||||
|
||||
<string name="clear">Start new game</string>
|
||||
<string name="undo">Undo last round</string>
|
||||
<string name="choose_theme_text">Choose theme</string>
|
||||
@@ -11,4 +11,11 @@
|
||||
<string name="confirmClear">Do you really want to delete the current game?</string>
|
||||
<string name="yes">Yes</string>
|
||||
<string name="no">No</string>
|
||||
<string name="back">Back</string>
|
||||
<string name="choose_language_text">Choose language</string>
|
||||
<string name="android_default_text">Android Default</string>
|
||||
<string name="english">English</string>
|
||||
<string name="german">German</string>
|
||||
<string name="light">Light</string>
|
||||
<string name="dark">Dark</string>
|
||||
</resources>
|
||||
5
app/src/main/res/xml/locales_config.xml
Normal file
5
app/src/main/res/xml/locales_config.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<locale android:name="en"/>
|
||||
<locale android:name="de"/>
|
||||
</locale-config>
|
||||
Reference in New Issue
Block a user