Multi-Language Support

As we all know Android phones are used by all kinds of people. So making apps in their language will make the user comfortable.

Watch this here


This is what we are going to show to users according to their preference

                
    Welcome to "CodeAndroid"       // English

    "CodeAndroid" க்கு வருக         // Tamil
            
    "CodeAndroid" में आपका स्वागत है    // Hindi

    Bienvenue dans "CodeAndroid"     // French

    Bienvenido a "CodeAndroid"       // Spanish
    
                
We need to keep equalent language text in its appropriate string.xml file under our "res" folder. Also we must add the string resource link in XML file.
                
    /res/values/string.xml
        <string name="welcome">Welcome to "CodeAndroid"</string>

    /res/values-ta/string.xml
        <string name="welcome">"CodeAndroid" க்கு வருக</string>

    /res/values-hi/string.xml
        <string name="welcome">"CodeAndroid" में आपका स्वागत है</string>

    /res/values-fr/string.xml
        <string name="welcome">Bienvenue dans "CodeAndroid"</string>

    /res/values-es/string.xml
        <string name="welcome">Bienvenido a "CodeAndroid"</string>


                

Database Insertion :

we have created a method called "insertData" for inserting data into database. We need to pass a ContentValue for inserting data. After inserting we will get the last inserted row id as result. So I am checking the result. "insert" will return -1 when something goes wrong while insertion.
                
    import android.os.Build
    import android.annotation.TargetApi
    import android.content.Context
    import android.content.ContextWrapper
    import android.content.res.Configuration
    import java.util.*


    class MyContextWrapper(base: Context) : ContextWrapper(base) {
        companion object {

            @Suppress("DEPRECATION")
            fun wrap(ctx: Context, language: String): ContextWrapper {
                var context = ctx
                val config = context.resources.configuration
                val sysLocale: Locale?
                sysLocale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    getSystemLocale(config)
                } else {
                    getSystemLocaleLegacy(config)
                }
                if (language != "" && sysLocale.language != language) {
                    val locale = Locale(language)
                    Locale.setDefault(locale)
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        setSystemLocale(config, locale)
                    } else {
                        setSystemLocaleLegacy(config, locale)
                    }

                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    context = context.createConfigurationContext(config)
                } else {
                    context.resources.updateConfiguration(config, context.resources.displayMetrics)
                }
                return MyContextWrapper(context)
            }

            @Suppress("DEPRECATION")
            private fun getSystemLocaleLegacy(config: Configuration): Locale {
                return config.locale
            }

            @TargetApi(Build.VERSION_CODES.N)
            fun getSystemLocale(config: Configuration): Locale {
                return config.locales.get(0)
            }

            @Suppress("DEPRECATION")
            private fun setSystemLocaleLegacy(config: Configuration, locale: Locale) {
                config.locale = locale
            }

            @TargetApi(Build.VERSION_CODES.N)
            fun setSystemLocale(config: Configuration, locale: Locale) {
                config.setLocale(locale)
            }
        }
    }
 
                

12800 Subscribers