Jetpack compose: How to get device primary language

This is a Jetpack Compose code snippet that displays the primary language of the device on which the app is running.

Here's a breakdown of the code:

  1. Import statements: The code imports necessary classes from libraries like androidx.activity.compose and androidx.compose.material.

  2. MainActivity class: This class is the main activity of the app. The onCreate function is called when the activity is first created. Inside the onCreate function, the setContent function is used to set the content of the activity. The content is a composable function called GetScaffold.

  3. GetScaffold composable: This composable function creates the overall layout of the app. It uses a Scaffold composable to provide a top bar and a content area.

  4. TopAppBar composable: This composable function creates the top bar of the app. It sets the title of the top bar to "Compose - Get Primary Language" and sets the background color of the top bar to a blue color.

  5. MainContent composable: This composable function displays the primary language of the device. It retrieves the current configuration using LocalConfiguration.current and then gets the list of locales from the configuration. The first locale in the list is considered the primary locale. The composable function then extracts the display language, country, display name, and language code from the primary locale and displays them on the screen using Text composables.

In summary, this code snippet demonstrates how to use Jetpack Compose to retrieve and display the primary language of the device. This can be useful for apps that need to localize their content or UI based on the user's language.


MainActivity.kt

package com.cfsuman.jetpackcompose

import android.os.Build
import android.os.Bundle
import android.os.LocaleList
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp


class MainActivity : ComponentActivity() {
    @RequiresApi(Build.VERSION_CODES.N)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {GetScaffold()}
    }


    @RequiresApi(Build.VERSION_CODES.N)
    @Composable
    fun GetScaffold() {
        Scaffold(
            topBar = {TopAppBar(
                title = {Text(text = "Compose - Get Primary Language")},
                backgroundColor = Color(0xFF7CB9E8),
            )},
            content = { MainContent() },
            backgroundColor = Color(0xFFF0FFFF)
        )
    }


    @RequiresApi(Build.VERSION_CODES.N)
    @Composable
    fun MainContent() {
        val configuration = LocalConfiguration.current
        val locales:LocaleList = configuration.locales

        Column(Modifier.padding(12.dp)) {
            Text(
                text = "Display Language : ${locales.get(0).displayLanguage}",
                fontSize = 20.sp
            )
            Text(
                text = "Country : ${locales.get(0).country}",
                fontSize = 20.sp
            )
            Text(
                text = "Display Name : ${locales.get(0).displayName}",
                fontSize = 20.sp
            )
            Text(
                text = "Language : ${locales.get(0).language}",
                fontSize = 20.sp
            )
        }
    }
}
More android jetpack compose tutorials