Android 14 (Source: https://android-developers.googleblog.com/2023/04/android-14-beta-1.html)
Check out Part 1 if you haven’t already to read more about Android 14’s Screenshot Detection API, Selected Photo Access, and Back Gesture Preview.
This article is a continuation of the “Android 14 — What’s New?” series. We will take a closer look at some of the features and updates in Android 14. This is a non-exhaustive list and only covers some of the more exciting new developments. These are based on what’s available in Android 14 Beta 1 and the UpsideDownCake Preview revision 3. These APIs could change between the time of writing and the final release.
Custom Actions in Intent Chooser
In a bid to have a bit more consistency across apps, Google has added a new API in Android 14 that let developers put custom actions in the standard Intent Resolver sheet that pops up for Chooser Intent or Share Intent.
Inconsistent custom share sheets (Source: https://blog.esper.io/android-14-share-menu/)
The API is fairly straightforward to use. If you are already using Intent.createChooser()
to create your Share Action Sheet, you need to simply add an array of custom actions to your intent. Essentially you create a CustomAction
using a builder pattern that accepts an Icon
, String
label and a PendingIntent
to perform the action when the Custom Action is tapped.
val customActionOne = ChooserAction.Builder( | |
Icon.createWithResource(applicationContext, R.drawable.outline_android), | |
getString(R.string.app_name), | |
PendingIntent.getActivity( | |
applicationContext, | |
/* Request code for the pending intent */ REQUEST_CODE, | |
/* Intent for the custom action */ intent, | |
PendingIntent.FLAG_IMMUTABLE | |
) | |
).build() | |
val customActionTwo = ChooserAction.Builder( | |
Icon.createWithResource(applicationContext, R.drawable.baseline_android), | |
getString(R.string.app_name), | |
PendingIntent.getActivity( | |
applicationContext, | |
/* Request code for the pending intent */ REQUEST_CODE, | |
/* Intent for the custom action */ intent, | |
PendingIntent.FLAG_IMMUTABLE | |
) | |
).build() | |
val chooserIntent = Intent.createChooser( | |
// your share intent goes here | |
).apply { | |
putExtra(Intent.EXTRA_CHOOSER_CUSTOM_ACTIONS, arrayOf(customActionOne, customActionTwo)) | |
} | |
startActivity(chooserIntent) |
Grammatical Inflection API
When addressing users, using the incorrect grammatical gender — for example, referring to women with masculine language — can have a negative impact on their performance and attitude. However, if the UI language reflects the user’s grammatical gender accurately, it can enhance user engagement and create a more customized and authentic user experience.
With Android 14 the Grammatical Inflection API lets developers add support for grammatical gender without refactoring the app. This makes it easier to localise apps in languages that have gender-based inflections for words e.g. French and Spanish.
With this API developers are required to create new resource buckets and use a new system service, called GrammaticalInflectionManager
.
// Get GrammaticalInflectionManager system service | |
val grammaticalInflectionManager = context.getSystemService(GrammaticalInflectionManager::class.java) | |
/** | |
* Use service to change the inflection of the app | |
* Options are | |
* Configuration.GRAMMATICAL_GENDER_NEUTRAL | |
* Configuration.GRAMMATICAL_GENDER_FEMININE | |
* Configuration.GRAMMATICAL_GENDER_MASCULINE | |
*/ | |
grammaticalInflectionManager.setRequestedApplicationGrammaticalGender( | |
Configuration.GRAMMATICAL_GENDER_NEUTRAL) |
Job Offers
As a best practice, if your app has an onboarding flow, that would be the best time to request the user’s gender and set the inflection accordingly.
If at any time your app needs to know the current inflection, you can get it using the system service
Demo for Grammatical Inflection API
// Get GrammaticalInflectionManager system service | |
val grammaticalInflectionManager = context.getSystemService(GrammaticalInflectionManager::class.java) | |
/** | |
* Use service to get inflection of the app | |
* Will be one of | |
* Configuration.GRAMMATICAL_GENDER_NEUTRAL | |
* Configuration.GRAMMATICAL_GENDER_FEMININE | |
* Configuration.GRAMMATICAL_GENDER_MASCULINE | |
*/ | |
grammaticalInflectionManager.getApplicationGrammaticalGender() |
Consider for a moment that your app needs to support English, French, and Spanish localisation along with inflections. The required buckets in this case would look like this
res/ | |
values/ | |
strings.xml | |
values-en-neuter/ | |
strings.xml | |
values-en-masculine/ | |
strings.xml | |
values-en-feminine/ | |
strings.xml | |
values-fr-neuter/ | |
strings.xml | |
values-fr-masculine/ | |
strings.xml | |
values-fr-feminine/ | |
strings.xml | |
values-es-neuter/ | |
strings.xml | |
values-es-masculine/ | |
strings.xml | |
values-es-feminine/ | |
strings.xml |
Each of these strings will contain the appropriate translation with the correct inflection.
You then simply use the string resource as you would normally do. The Android system then picks the correct resource based on language and inflection values for the app.
Calling
setRequestedApplicationGrammaticalGender()
recreates yourActivity
, unless your app handlesgrammaticalGender
configuration changes by itself.
Support for these buckets is only available in Android Studio Giraffe Carany 7 or higher.
Regional Preferences
Android 14 adds a new screen in the Settings app. This screen lets the user set/override Locale preferences like First Day of the Week, Temperature Unit, and Numbers Preference.
Regional Preferences in Android 14
The APIs in discussion however are part of AndroidX Core library. Starting with 1.12.0-alpha01, the API lets you fetch the above-mentioned user preferences.
You will need to include androidx.core:core:1.12-alpha01
or android.core:core-ktx:1.12-alpha01
in your dependencies to be able to access the following functions.
/** | |
* More information at https://developer.android.com/reference/androidx/core/text/util/LocalePreferences | |
*/ | |
// Calendar Type | |
LocalePreferences.getCalendarType() | |
// First day of the week | |
LocalePreferences.getFirstDayOfWeek() | |
// Hour Cycle | |
LocalePreferences.getHourCycle() | |
// Temperature Unit | |
LocalePreferences.getTemperatureUnit() |
Functions available to read user preferences based on Locale
Please note that at the time of writing, there is no public API to fetch the Numbers Preference of a user.
Read more about this feature at Android Developers
All the new APIs mentioned above in this article and the second article can be checked out using the sample app on GitHub.
GitHub — kartikarora/android-14: Working demo of new things in Android 14
For regular updates and more Android goodness, follow me on my socials and follow me on Medium while you are at it!
Kartik Arora (@Kartikarora@androiddev.social)
If you want to support me with the work I do, feel free to buy me a coffee!
This article was previously published on proandroiddev.com