Regional Preferences-Android14

Nav Singh
ProAndroidDev
Published in
3 min readMar 23, 2023

Introducing Android14’s Regional Preferences API, which allows users to set their regional preferences.

With regional preferences, users can customize temperature units, date formats, and numbering systems.

It might be more convenient for an American living in Europe to have temperatures represented in Fahrenheit rather than Celsius and to have apps treat Sunday as the start of the week instead of Monday.

Requirement

 implementation 'androidx.core:core-ktx:1.12.0-alpha01'

LocalePreferences only available starting from this version.

Step-by-step instructions for customizing preferences

Accessing preferences

  • Accessing the first day of the week 🗓️
LocalePreferences.getFirstDayOfWeek()
  • Accessing hour cycle ⏱️
LocalePreferences.getHourCycle()
  • Accessing temperature unit ⛅️
LocalePreferences.getTemperatureUnit() 
  • Accessing calendar type 🗓️
LocalePreferences.getCalendarType()

Using these new APIs: Use Cases

  1. As an example, let’s build a weather app ⛅️ that displays weather information in °C format. Or, maybe we provide a setting to change the unit (°C °F) in-app.
  • Now that we have LocalePreferences.getTemperatureUnit(), we can provide a better user experience.
Text(
text = when (LocalePreferences.getTemperatureUnit()) {
LocalePreferences.TemperatureUnit.CELSIUS -> "20 ℃"
LocalePreferences.TemperatureUnit.FAHRENHEIT -> "68 ℉"
else -> {""}
},

2. Now let’s say we have an app where we have weekly challenges and we show how many days are left in the current week.

In that case, we can use LocalePreferences.getFirstDayOfWeek() to calculate the number of days left based on the first day of the week selected in preferences.

val localDate = LocalDate.now()
// To learn more about WeekFields check the references
val weekField = WeekFields.of(Locale.getDefault())
// day number based on the preferences
Text(text = "${localDate.get(weekField.dayOfWeek())}")

Demo

  • Changing preferences
  • Application

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Nav Singh

Google Developer Expert for Android | Mobile Software Engineer at Manulife | Organizer at GDG Montreal

No responses yet

What are your thoughts?