|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.views_app |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import android.os.Bundle |
| 21 | +import androidx.appcompat.app.AppCompatActivity |
| 22 | +import androidx.appcompat.app.AppCompatDelegate |
| 23 | +import androidx.core.os.LocaleListCompat |
| 24 | +import com.example.views_app.databinding.ActivityMainBinding |
| 25 | +import java.util.* |
| 26 | + |
| 27 | +class MainActivity : AppCompatActivity() { |
| 28 | + |
| 29 | + companion object { |
| 30 | + const val PREFERENCE_NAME = "shared_preference" |
| 31 | + const val PREFERENCE_MODE = Context.MODE_PRIVATE |
| 32 | + |
| 33 | + const val FIRST_TIME_MIGRATION = "first_time_migration" |
| 34 | + const val SELECTED_LANGUAGE = "selected_language" |
| 35 | + |
| 36 | + const val STATUS_DONE = "status_done" |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * This is a sample code that explains the use of getter and setter APIs for Locales introduced |
| 41 | + * in the Per-App language preferences. Here is an example use of the AndroidX Support Library |
| 42 | + * */ |
| 43 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 44 | + super.onCreate(savedInstanceState) |
| 45 | + val binding = ActivityMainBinding.inflate(layoutInflater) |
| 46 | + val view = binding.root |
| 47 | + setContentView(view) |
| 48 | + |
| 49 | + /* NOTE: If you were handling the locale storage on you own earlier, you will need to add a |
| 50 | + one time migration for switching this storage from a custom way to the AndroidX storage. |
| 51 | +
|
| 52 | + This can be done in the following manner. Lets say earlier the locale preference was |
| 53 | + stored in a SharedPreference */ |
| 54 | + |
| 55 | + // Check if the migration has already been done or not |
| 56 | + if (getString(FIRST_TIME_MIGRATION) != STATUS_DONE) { |
| 57 | + // Fetch the selected language from wherever it was stored. In this case its SharedPref |
| 58 | + getString(SELECTED_LANGUAGE)?.let { |
| 59 | + // Set this locale using the AndroidX library that will handle the storage itself |
| 60 | + val localeList = LocaleListCompat.forLanguageTags(it) |
| 61 | + AppCompatDelegate.setApplicationLocales(localeList) |
| 62 | + // Set the migration flag to ensure that this is executed only once |
| 63 | + putString(FIRST_TIME_MIGRATION, STATUS_DONE) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Fetching the current application locale using the AndroidX support Library |
| 68 | + val currentLocaleName = if (!AppCompatDelegate.getApplicationLocales().isEmpty) { |
| 69 | + // Fetches the current Application Locale from the list |
| 70 | + AppCompatDelegate.getApplicationLocales()[0]?.displayName |
| 71 | + } else { |
| 72 | + // Fetches the default System Locale |
| 73 | + Locale.getDefault().displayName |
| 74 | + } |
| 75 | + |
| 76 | + // Displaying the selected locale on screen |
| 77 | + binding.tvSelectedLanguage.text = currentLocaleName |
| 78 | + |
| 79 | + // Setting app language to "English" in-app using the AndroidX support library |
| 80 | + binding.btnSelectEnglish.setOnClickListener { |
| 81 | + val localeList = LocaleListCompat.forLanguageTags("en") |
| 82 | + AppCompatDelegate.setApplicationLocales(localeList) |
| 83 | + } |
| 84 | + |
| 85 | + // Setting app language to "Hindi" in-app using the AndroidX support library |
| 86 | + binding.btnSelectHindi.setOnClickListener { |
| 87 | + val localeList = LocaleListCompat.forLanguageTags("hi") |
| 88 | + AppCompatDelegate.setApplicationLocales(localeList) |
| 89 | + } |
| 90 | + |
| 91 | + // Setting app language to "Arabic" in-app using the AndroidX support Library |
| 92 | + // NOTE: Here the screen orientation is reversed to RTL |
| 93 | + binding.btnSelectArabic.setOnClickListener { |
| 94 | + val localeList = LocaleListCompat.forLanguageTags("ar") |
| 95 | + AppCompatDelegate.setApplicationLocales(localeList) |
| 96 | + } |
| 97 | + |
| 98 | + // Setting app language to "Japanese" in-app using the AndroidX support library |
| 99 | + binding.btnSelectJapanese.setOnClickListener { |
| 100 | + val localeList = LocaleListCompat.forLanguageTags("ja") |
| 101 | + AppCompatDelegate.setApplicationLocales(localeList) |
| 102 | + } |
| 103 | + |
| 104 | + // Setting app language to "Spanish" in-app using the AndroidX support library |
| 105 | + binding.btnSelectSpanish.setOnClickListener { |
| 106 | + val localeList = LocaleListCompat.forLanguageTags("es") |
| 107 | + AppCompatDelegate.setApplicationLocales(localeList) |
| 108 | + } |
| 109 | + |
| 110 | + // Setting app language to Traditional Chinese in-app using the AndroidX support Library |
| 111 | + binding.btnSelectXxYy.setOnClickListener { |
| 112 | + val localeList = LocaleListCompat.forLanguageTags("zh-Hant") |
| 113 | + AppCompatDelegate.setApplicationLocales(localeList) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private fun putString(key: String, value: String) { |
| 118 | + val editor = getSharedPreferences(PREFERENCE_NAME, PREFERENCE_MODE).edit() |
| 119 | + editor.putString(key, value) |
| 120 | + editor.apply() |
| 121 | + } |
| 122 | + |
| 123 | + private fun getString(key: String): String? { |
| 124 | + val preference = getSharedPreferences(PREFERENCE_NAME, PREFERENCE_MODE) |
| 125 | + return preference.getString(key, null) |
| 126 | + } |
| 127 | +} |
0 commit comments