Creating a Custom Chooser

Sometimes you want to modify some of the defaults that appear in the chooser, like when you're sharing a text or image to any other android app that can accept text or images. The most common use case for this is when users press the "Share" button in your app.

To create the custom chooser, you use the ShareCompat.IntentBuilder class.

String title = "Share weather details";
String mimeType = "text/plain";
String shareText = "I want to share this string";

Intent intent = ShareCompat.IntentBuilder.from(this)
    .setType(mimeType)
    .setChooserTitle(title)
    .setText(shareText)
    .getIntent();
if (intent.resolveActivity(getPackageManager()) != null)
    startActivity(intent);

When it comes to MIME types, Mozilla has a good reference article on them and a list of common ones. It's web focused but the principals apply to android as well. I also liked this SO answer on creating custom MIME types in android.

Posted by Taylor