Skip to main content

Custom Actions in Android Sharesheet

warning

Custom actions are only available in API level 34 (Android 14) or higher.

Android 14 enables you to add custom actions to your Android Sharesheet.

Basic Usage​

To add custom action, simply add EXTRA_CHOOSER_CUSTOM_ACTIONS extra to the share intent. The extra value should be a list of ChoiceAction instances. Each ChoiceAction has its own PendingIntent that will be invoked when the custom action is clicked:

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Icon
import android.net.Uri
import android.os.Build
import android.service.chooser.ChooserAction

// Define Android Sharesheet intent.
val sendIntent = Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(
Intent.EXTRA_TEXT,
"Visit https://android-notebook.hanmajid.com !"
)
val shareIntent = Intent.createChooser(sendIntent, null)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
// Define PendingIntent to be invoked when custom action is clicked.
val customIntent = Intent(
Intent.ACTION_VIEW,
Uri.parse("https://android-notebook.hanmajid.com"),
)
val pendingIntent = PendingIntent.getActivity(
context,
1,
customIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
)

// Add custom actions as an extra.
val customActions = arrayOf(
ChooserAction.Builder(
Icon.createWithResource(
context,
R.drawable.ic_launcher_foreground,
),
"Open Android Notebook!",
pendingIntent,
).build()
)
shareIntent.putExtra(
Intent.EXTRA_CHOOSER_CUSTOM_ACTIONS,
customActions
)
}
context.startActivity(shareIntent)

The above code defines a custom action "Open Android Notebook" that will open this website when clicked. Here's how it looks in action:

References​