Blog Infos
Author
Published
Topics
Author
Published

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

With regional preferences, users can customize temperature unitsdate 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 -> {""}
    },

 

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Migrating to Jetpack Compose – an interop love story

Most of you are familiar with Jetpack Compose and its benefits. If you’re able to start anew and create a Compose-only app, you’re on the right track. But this talk might not be for you…
Watch Video

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer for Jetpack Compose
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engin ...
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer f ...
Google

Jobs

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

This article was previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Grammatical Inflection API provides a more personalized, natural-sounding user experience for users speaking languages…
READ MORE
blog
Check out Part 1 if you haven’t already to read more about Android 14’s…
READ MORE
blog
Android 14 introduces a privacy-preserving screenshot detection API to create a more standardized screenshot…
READ MORE
blog
Android 14 is already here, so I took the documentation, experts’ reviews, and other…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu