ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Regional Preferences-Android14

--

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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

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

Write a response