Blog Infos
Author
Published
Topics
Author
Published

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
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 using Intent.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
    }
}

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

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