Blog Infos
With the release of the first beta of Android14, we got a new feature called CustomActions for Sharesheet.
Here we will explore this new feature and see how we can add our own custom actions to Sharesheet.
In Android, we will see the following standard Sharesheet with several options whenever we want to share something.
Standard Sharesheet
- There are quick actions and apps that can handle the content that we want to share
Let’s add some custom actions
- With the help of a new
ChooserAction API, We can define our own
ChooserAction
- We will use
ChooserAction.Builder to build
ChooserAction
Implementation
Create a Chooser action
ChooserAction.Builder takes 3 parameters —
Icon, Label, PendingIntent
- Create a
PendingIntent
val pendingIntent = PendingIntent.getActivity( context, 0, Intent(Intent.ACTION_WEB_SEARCH).apply { putExtra(SearchManager.QUERY, "Search on web 🌐.") }, PendingIntent.FLAG_IMMUTABLE )
- Create a
chooser action using the
PendingIntent that we have created in the last step 👆
val customAction = ChooserAction.Builder( // Icon Icon.createWithResource(context, R.drawable.baseline_screen_share_24), // Label "CustomAction-Android14", // PendingIntent pendingIntent ).build()
- Create an
Intent for
Chooser
val intent = Intent(Intent.ACTION_SEND).apply { type = "text/plain" putExtra(Intent.EXTRA_TEXT, "Sample text to send.") }
- Create an Chooser using
Intent 👆
val chooserIntent = Intent.createChooser(intent, "Android-14").
- Add
custom actions to chooser intent 👆
Add custom action to the
intent
usingIntent.EXTRA_CHOOSER_CUSTOM_ACTIONS
We need to pass custom actions as an
array
even if we have single custom action.
val chooserIntent = Intent.createChooser(intent, "Android-14").apply { // We need to pass an array putExtra(Intent.EXTRA_CHOOSER_CUSTOM_ACTIONS, arrayOf(customAction)) }
Composable with all the code
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // 1. Create chooser actions val chooserActions = buildCustomShareSheetActions(this) // 2. Create chooser intent val chooserIntent = buildChooserIntent(chooserActions) setContent { GrammaticalInflectionAPITheme { Surface(modifier = Modifier, color = MaterialTheme.colorScheme.background) { MainComposable { // Launch chooser intent startActivity(chooserIntent) } } } } } private fun buildCustomShareSheetActions(context: Context): Array<ChooserAction> { val pendingIntent = PendingIntent.getActivity( context, 0, Intent(Intent.ACTION_WEB_SEARCH).apply { putExtra(SearchManager.QUERY, "Search on web 🌐") }, PendingIntent.FLAG_IMMUTABLE ) val customAction = ChooserAction.Builder( Icon.createWithResource(context, R.drawable.baseline_screen_share_24), "CustomAction-Android14", pendingIntent ).build() return arrayOf(customAction) } private fun buildChooserIntent(chooserActions: Array<ChooserAction>): Intent { val intent = Intent(Intent.ACTION_SEND).apply { type = "text/plain" putExtra(Intent.EXTRA_TEXT, "Sample text to send.") } val chooserIntent = Intent.createChooser(intent, "Android-14").apply { putExtra(Intent.EXTRA_CHOOSER_CUSTOM_ACTIONS, chooserActions) } return chooserIntent } }