From 2e6a8bb22d3630bfa74fe6d5b448d3d7763fcfc8 Mon Sep 17 00:00:00 2001 From: Sarac Date: Thu, 6 May 2021 11:02:24 +0200 Subject: [PATCH] TL-013 change language for each activity [MI, SM] --- .../com/example/tutorly/FilterActivity.kt | 28 ++++++++++++++++ .../java/com/example/tutorly/LoginActivity.kt | 15 ++++----- .../java/com/example/tutorly/MainActivity.kt | 33 ++++++++++++++++++- .../java/com/example/tutorly/TownSelection.kt | 32 +++++++++++++++++- .../java/com/example/tutorly/Translation.kt | 23 +++++++++++++ .../src/main/res/layout/activity_filter.xml | 24 ++++++++++---- .../app/src/main/res/layout/activity_main.xml | 15 +++++++-- .../res/layout/activity_town_selection.xml | 11 +++++++ .../src/main/res/values-kv-rRU/strings.xml | 2 ++ Tutorly/app/src/main/res/values/strings.xml | 2 ++ 10 files changed, 166 insertions(+), 19 deletions(-) create mode 100644 Tutorly/app/src/main/java/com/example/tutorly/Translation.kt diff --git a/Tutorly/app/src/main/java/com/example/tutorly/FilterActivity.kt b/Tutorly/app/src/main/java/com/example/tutorly/FilterActivity.kt index 37747c6..71b97e2 100644 --- a/Tutorly/app/src/main/java/com/example/tutorly/FilterActivity.kt +++ b/Tutorly/app/src/main/java/com/example/tutorly/FilterActivity.kt @@ -1,8 +1,11 @@ package com.example.tutorly +import android.app.AlertDialog +import android.content.Intent import android.media.Image import android.os.Bundle import android.util.Log +import android.widget.Button import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.LinearLayoutManager @@ -20,6 +23,7 @@ class FilterActivity : AppCompatActivity() { setContentView(R.layout.activity_filter) val availableSubjects: ArrayList = ArrayList() + val changeLang: Button = findViewById(R.id.btn_change_lang_filter) /* TODO: * retrieve subjects from database!!! @@ -41,6 +45,30 @@ class FilterActivity : AppCompatActivity() { filterRecyclerView.itemAnimator = DefaultItemAnimator() subjectAdapter = RecyclerViewAdapter(this, availableSubjects) filterRecyclerView.adapter = subjectAdapter + + changeLang.setOnClickListener{ + val list = arrayOf("English", "Russian") + val builder = AlertDialog.Builder(this@FilterActivity) + builder.setTitle(R.string.choose_lang) + + builder.setSingleChoiceItems(list, -1) { dialog, which -> + if (which == 0) { + Translation().changeLang("default", this) + recreate() + } + else if (which == 1) { + Translation().changeLang("kv", this) + recreate() + } + dialog.dismiss() + } + builder.show() + } + } + + override fun onBackPressed() { + val intent = Intent(this, MainActivity::class.java) + startActivity(intent) } diff --git a/Tutorly/app/src/main/java/com/example/tutorly/LoginActivity.kt b/Tutorly/app/src/main/java/com/example/tutorly/LoginActivity.kt index 13d1569..e8142f2 100644 --- a/Tutorly/app/src/main/java/com/example/tutorly/LoginActivity.kt +++ b/Tutorly/app/src/main/java/com/example/tutorly/LoginActivity.kt @@ -2,6 +2,7 @@ package com.example.tutorly import android.app.Activity import android.app.AlertDialog +import android.content.Context import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle @@ -42,17 +43,18 @@ class LoginActivity : AppCompatActivity() { val inputPassword: EditText = findViewById(R.id.input_password_login) changeLang.setOnClickListener { - val list = arrayOf("English", "Russian") val builder = AlertDialog.Builder(this@LoginActivity) builder.setTitle(R.string.choose_lang) builder.setSingleChoiceItems(list, -1) { dialog, which -> if (which == 0) { - changeLang("default") + Translation().changeLang("default", this) + recreate() } else if (which == 1) { - changeLang("kv") + Translation().changeLang("kv", this) + recreate() } dialog.dismiss() } @@ -112,16 +114,13 @@ class LoginActivity : AppCompatActivity() { } } - private fun changeLang(localeName: String) { + fun changeLang(localeName: String, context: Context) { locale = Locale(localeName) - val res = resources + val res = context.resources val dm = res.displayMetrics val conf = res.configuration conf.locale = locale res.updateConfiguration(conf, dm) - val intent = Intent( - this, LoginActivity::class.java) - startActivity(intent) } } \ No newline at end of file diff --git a/Tutorly/app/src/main/java/com/example/tutorly/MainActivity.kt b/Tutorly/app/src/main/java/com/example/tutorly/MainActivity.kt index 8e5985c..d9ff26d 100644 --- a/Tutorly/app/src/main/java/com/example/tutorly/MainActivity.kt +++ b/Tutorly/app/src/main/java/com/example/tutorly/MainActivity.kt @@ -1,10 +1,12 @@ package com.example.tutorly +import android.app.AlertDialog import android.content.Intent -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button +import androidx.appcompat.app.AppCompatActivity +import kotlin.system.exitProcess class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -12,17 +14,46 @@ class MainActivity : AppCompatActivity() { setContentView(R.layout.activity_main) val button: Button = findViewById(R.id.btnTownSelection) + val changeLang: Button = findViewById(R.id.btn_change_lang_main_activity) button.setOnClickListener { val intent = Intent(this, TownSelection::class.java) startActivity(intent); } + + changeLang.setOnClickListener { + val list = arrayOf("English", "Russian") + val builder = AlertDialog.Builder(this@MainActivity) + builder.setTitle(R.string.choose_lang) + + builder.setSingleChoiceItems(list, -1) { dialog, which -> + if (which == 0) { + Translation().changeLang("default", this) + recreate() + } + else if (which == 1) { + Translation().changeLang("kv", this) + recreate() + } + dialog.dismiss() + } + builder.show() + } } + + fun switchToFilter(view: View) { val switchIntent = Intent(this, FilterActivity::class.java).apply() { } startActivity(switchIntent) } + + override fun onBackPressed() { + + } + + + } \ No newline at end of file diff --git a/Tutorly/app/src/main/java/com/example/tutorly/TownSelection.kt b/Tutorly/app/src/main/java/com/example/tutorly/TownSelection.kt index e5e8ce0..0b009e9 100644 --- a/Tutorly/app/src/main/java/com/example/tutorly/TownSelection.kt +++ b/Tutorly/app/src/main/java/com/example/tutorly/TownSelection.kt @@ -1,6 +1,8 @@ package com.example.tutorly import android.Manifest +import android.app.AlertDialog +import android.content.Intent import android.content.pm.PackageManager import android.os.Bundle import android.widget.Button @@ -19,6 +21,7 @@ import com.google.android.libraries.places.widget.listener.PlaceSelectionListene import com.google.android.libraries.places.widget.model.AutocompleteActivityMode import java.util.* import javax.net.ssl.SSLEngineResult +import kotlin.system.exitProcess class TownSelection : AppCompatActivity() { @@ -35,8 +38,30 @@ class TownSelection : AppCompatActivity() { setContentView(R.layout.activity_town_selection) setSupportActionBar(findViewById(R.id.titleToolbar)) + var changeLang: Button = findViewById(R.id.btn_change_lang_town_selection) placesAutocomplete() + + + changeLang.setOnClickListener{ + val list = arrayOf("English", "Russian") + val builder = AlertDialog.Builder(this@TownSelection) + builder.setTitle(R.string.choose_lang) + + builder.setSingleChoiceItems(list, -1) { dialog, which -> + if (which == 0) { + Translation().changeLang("default", this) + recreate() + } + else if (which == 1) { + Translation().changeLang("kv", this) + recreate() + } + dialog.dismiss() + } + builder.show() + } + } @@ -48,7 +73,7 @@ class TownSelection : AppCompatActivity() { autocompleteFragment.setPlaceFields(placeFields) - autocompleteFragment.setHint("City..") + autocompleteFragment.setHint(resources.getString(R.string.city_hint)) autocompleteFragment.setOnPlaceSelectedListener(object : PlaceSelectionListener { override fun onPlaceSelected(p0: Place) { @@ -65,5 +90,10 @@ class TownSelection : AppCompatActivity() { }) } + override fun onBackPressed() { + val intent = Intent(this, MainActivity::class.java) + startActivity(intent) + } + } diff --git a/Tutorly/app/src/main/java/com/example/tutorly/Translation.kt b/Tutorly/app/src/main/java/com/example/tutorly/Translation.kt new file mode 100644 index 0000000..e3fc0c2 --- /dev/null +++ b/Tutorly/app/src/main/java/com/example/tutorly/Translation.kt @@ -0,0 +1,23 @@ +package com.example.tutorly + +import android.app.Activity +import android.app.AlertDialog +import android.content.Context +import android.content.Intent +import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat.startActivity +import java.util.* + +class Translation : AppCompatActivity(){ + + lateinit var locale: Locale + + fun changeLang(localeName: String, context: Context) { + locale = Locale(localeName) + val res = context.resources + val dm = res.displayMetrics + val conf = res.configuration + conf.locale = locale + res.updateConfiguration(conf, dm) + } +} \ No newline at end of file diff --git a/Tutorly/app/src/main/res/layout/activity_filter.xml b/Tutorly/app/src/main/res/layout/activity_filter.xml index 2241e86..da4e1a6 100644 --- a/Tutorly/app/src/main/res/layout/activity_filter.xml +++ b/Tutorly/app/src/main/res/layout/activity_filter.xml @@ -14,25 +14,35 @@ android:orientation="horizontal"> + android:text="@string/filterRadioBtnLvl1" /> + + android:text="@string/filterRadioBtnLvl2" /> +