'How to change spinner ArrayList options to buttons? [KOTLIN]
I am new in android and kotlin. I want to change the Arraylist options that is in an spinner to buttons, I am talking about language options that is in spinner, but I don't want to be in spinner. there was nothing related article that shows something like that, I tried to change the spinner to buttons but it was showing error and App crashed! I hope my question be clear to you.
1- this is my LanguageActivity.kt :
class LanguageActivity : AppCompatActivity() {
private lateinit var databinding: ActivityLanguageBinding
private lateinit var locale: Locale
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
databinding = DataBindingUtil.setContentView(this,R.layout.activity_language)
val languageList = ArrayList<String>()
languageList.add("Select")
languageList.add("English")
languageList.add("Persian")
languageList.add("Pashto")
val adapter = ArrayAdapter(this,
androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, languageList)
databinding.sLanguage.adapter = adapter
databinding.sLanguage.onItemSelectedListener = object :
AdapterView.OnItemSelectedListener{
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id:
Long) {
when(position){
0 -> {
}
1 -> setLocale("en")
2 -> setLocale("fa")
3 -> setLocale("ps")
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
}
fun setLocale (languageName: String){
locale = Locale(languageName)
val res = resources
val dm = res.displayMetrics
val conf = res.configuration
conf.locale = locale
res.updateConfiguration(conf,dm)
val refresh = Intent(this, LanguageActivity::class.java)
startActivity(refresh)
}
}
2- and this is the activity_language.xml :
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Spinner
android:id="@+id/sLanguage"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"/>
</LinearLayout>
</layout>
thank you for helping me. take care.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
