add lang toggle in setttings

This commit is contained in:
2026-05-12 11:27:37 +05:00
parent 4d5ff472d2
commit d28687bc1c
4 changed files with 54 additions and 0 deletions

View File

@@ -6,6 +6,8 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.AppCompatDelegate.setApplicationLocales
import androidx.core.os.LocaleListCompat
import androidx.fragment.app.Fragment
import sh.sar.basedbank.R
import sh.sar.basedbank.databinding.FragmentSettingsBinding
@@ -42,6 +44,19 @@ class SettingsFragment : Fragment() {
prefs.edit().putString("theme", key).apply()
AppCompatDelegate.setDefaultNightMode(mode)
}
// Language
val currentLocales = AppCompatDelegate.getApplicationLocales()
val currentLang = if (currentLocales.isEmpty) "en" else currentLocales[0]?.language ?: "en"
binding.languageToggle.check(
if (currentLang == "dv") R.id.btnLangDhivehi else R.id.btnLangEnglish
)
binding.languageToggle.addOnButtonCheckedListener { _, checkedId, isChecked ->
if (!isChecked) return@addOnButtonCheckedListener
val tag = if (checkedId == R.id.btnLangDhivehi) "dv" else "en"
setApplicationLocales(LocaleListCompat.forLanguageTags(tag))
}
}
override fun onResume() {

View File

@@ -52,6 +52,39 @@
</com.google.android.material.button.MaterialButtonToggleGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/language"
android:textAppearance="?attr/textAppearanceTitleMedium"
android:layout_marginTop="24dp"
android:layout_marginBottom="12dp" />
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/languageToggle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:singleSelection="true"
app:selectionRequired="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/btnLangEnglish"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/lang_english" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnLangDhivehi"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/lang_dhivehi" />
</com.google.android.material.button.MaterialButtonToggleGroup>
</LinearLayout>
</ScrollView>

View File

@@ -86,6 +86,9 @@
<string name="theme_system">ސިސްޓަމް</string>
<string name="theme_light">ލައިޓް</string>
<string name="theme_dark">ޑާކް</string>
<string name="language">ބަސް</string>
<string name="lang_english">English</string>
<string name="lang_dhivehi">ދިވެހި</string>
<!-- Home -->
<string name="accounts">އެކައުންޓްތައް</string>

View File

@@ -85,6 +85,9 @@
<string name="theme_system">System</string>
<string name="theme_light">Light</string>
<string name="theme_dark">Dark</string>
<string name="language">Language</string>
<string name="lang_english">English</string>
<string name="lang_dhivehi">ދިވެހި</string>
<!-- Home -->
<string name="accounts">Accounts</string>