Home > Mobile >  When I click on AlertDialog item, the radio group not stay ON when close and open alert dialog again
When I click on AlertDialog item, the radio group not stay ON when close and open alert dialog again

Time:03-28

I want to the Radiogroup stay ON when I click on an item from the alertdialog (My AlertDialog) and when I close and open alertdialog again, it still staying ON (Like This)

I also used [checkeditem: 0] to hold radiogroup always ON just for arrayof("English"). but I dont want be always on English Item, I want to it change by selecting each arrayof[("English","فارسی")]

here is my codes: 1- the MainActivity.kt:

import android.content.DialogInterface
import android.content.res.Configuration
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import java.util.*

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    loadLocale()
    setContentView(R.layout.activity_main)
    val actionBar = supportActionBar!!
    actionBar.title = resources.getString(R.string.app_name)
    val changeLang = findViewById<Button>(R.id.changeMyLang)
    changeLang.setOnClickListener { showChangeLanguageDialog() }
}

private fun showChangeLanguageDialog() {
    val mBuilder = AlertDialog.Builder(this@MainActivity)
    val listItems = arrayOf("English", "فارسی")
    mBuilder.setSingleChoiceItems(listItems, checkeditem: 0) { dialogInterface: DialogInterface, i: Int ->
        if (i == 0) {
            setLocale("en")
            recreate()
        } else if (i == 1) {
            setLocale("fa")
            recreate()
        }
        dialogInterface.dismiss()
    }
    val mDialog = mBuilder.create()
    mDialog.show()
}

private fun setLocale(lang: String?) {
    val locale = Locale(lang)
    Locale.setDefault(locale)
    val config = Configuration()
    config.locale = locale
    baseContext.resources.updateConfiguration(config, baseContext.resources.displayMetrics)
    val editor = getSharedPreferences("Settings", MODE_PRIVATE).edit()
    editor.putString("My_Lang", lang)
    editor.apply()
}

private fun loadLocale() {
    val prefs = getSharedPreferences("Settings", MODE_PRIVATE)
    val language = prefs.getString("My_Lang", "")
    setLocale(language)
}
}

2- the activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/welcome"
    android:textSize="30sp" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/email" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/password" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/login" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/signup" />

<Button
    android:id="@ id/changeMyLang"
    android:layout_marginTop="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/login" />

</LinearLayout>

I used dialogInterface.dismiss() and dialogInterface.cancel() but none of them hold the radiogroup ON. thank you for helping me. take care.

CodePudding user response:

Replace your method with below one,

private fun showChangeLanguageDialog() {
            val pref = getSharedPreferences("Settings", MODE_PRIVATE)
    
            val mBuilder = AlertDialog.Builder(this@MainActivity)
            val listItems = arrayOf("English", "فارسی")
    
            var checkedItem = 0
            if (pref.getString("My_Lang", "en").equals("en")) {
                checkedItem = 0
            } else if (pref.getString("My_Lang", "en").equals("fa")) {
                checkedItem = 1
            }
    
            mBuilder.setSingleChoiceItems(listItems, checkedItem) { dialogInterface: DialogInterface, i: Int ->
                if (i == 0) {
                    setLocale("en")
                    recreate()
                } else if (i == 1) {
                    setLocale("fa")
                    recreate()
                }
                dialogInterface.dismiss()
            }
            val mDialog = mBuilder.create()
            mDialog.show()
    }
  • Related