Google is committed to advancing racial equity for Black communities. See how.

RichContentReceiverCompat

public abstract class RichContentReceiverCompat
extends Object

java.lang.Object
   ↳ androidx.core.widget.RichContentReceiverCompat<T extends android.view.View>


Callback for apps to implement handling for insertion of rich content. "Rich content" here refers to both text and non-text content: plain text, styled text, HTML, images, videos, audio files, etc.

This callback can be attached to different types of UI components. For editable TextView components, implementations should typically extend from TextViewRichContentReceiverCompat.

Example implementation:

   public class MyRichContentReceiver extends TextViewRichContentReceiverCompat {

       private static final Set<String> SUPPORTED_MIME_TYPES = Collections.unmodifiableSet(
           Set.of("text/*", "image/gif", "image/png", "image/jpg"));

       @NonNull
       @Override
       public Set<String> getSupportedMimeTypes() {
           return SUPPORTED_MIME_TYPES;
       }

       @Override
       public boolean onReceive(@NonNull TextView textView, @NonNull ClipData clip,
               int source, int flags) {
         if (clip.getDescription().hasMimeType("image/*")) {
             return receiveImage(textView, clip);
         }
         return super.onReceive(textView, clip, source);
       }

       private boolean receiveImage(@NonNull TextView textView, @NonNull ClipData clip) {
           // ... app-specific logic to handle the content URI in the clip ...
       }
   }
 

Summary

Constants

int FLAG_CONVERT_TO_PLAIN_TEXT

Flag for onReceive(T, ClipData, int, int) requesting that the content should be converted to plain text prior to inserting.

int SOURCE_CLIPBOARD

Specifies that the operation was triggered by a paste from the clipboard (e.g.

int SOURCE_INPUT_METHOD

Specifies that the operation was triggered from the soft keyboard (also known as input method editor or IME).

Public constructors

RichContentReceiverCompat()

Public methods

abstract Set<String> getSupportedMimeTypes()

Returns the MIME types that can be handled by this callback.

abstract boolean onReceive(T view, ClipData clip, int source, int flags)

Insert the given clip.

Inherited methods

Constants

FLAG_CONVERT_TO_PLAIN_TEXT

public static final int FLAG_CONVERT_TO_PLAIN_TEXT

Flag for onReceive(T, ClipData, int, int) requesting that the content should be converted to plain text prior to inserting.

Constant Value: 1 (0x00000001)

SOURCE_CLIPBOARD

public static final int SOURCE_CLIPBOARD

Specifies that the operation was triggered by a paste from the clipboard (e.g. "Paste" or "Paste as plain text" action in the insertion/selection menu).

Constant Value: 0 (0x00000000)

SOURCE_INPUT_METHOD

public static final int SOURCE_INPUT_METHOD

Specifies that the operation was triggered from the soft keyboard (also known as input method editor or IME). See https://developer.android.com/guide/topics/text/image-keyboard for more info.

Constant Value: 1 (0x00000001)

Public constructors

RichContentReceiverCompat

public RichContentReceiverCompat ()

Public methods

getSupportedMimeTypes

public abstract Set<String> getSupportedMimeTypes ()

Returns the MIME types that can be handled by this callback.

Different platform features (e.g. pasting from the clipboard, inserting stickers from the keyboard, etc) may use this function to conditionally alter their behavior. For example, the keyboard may choose to hide its UI for inserting GIFs if the input field that has focus has a RichContentReceiverCompat set and the MIME types returned from this function don't include "image/gif".

Returns
Set<String> An immutable set with the MIME types supported by this callback. The returned MIME types may contain wildcards such as "text/*", "image/*", etc.

onReceive

public abstract boolean onReceive (T view, 
                ClipData clip, 
                int source, 
                int flags)

Insert the given clip.

For a UI component where this callback is set, this function will be invoked in the following scenarios:

  1. Paste from the clipboard (e.g. "Paste" or "Paste as plain text" action in the insertion/selection menu)
  2. Content insertion from the keyboard (InputConnection.commitContent(InputContentInfo, int, Bundle))

For text, if the view has a selection, the selection should be overwritten by the clip; if there's no selection, this method should insert the content at the current cursor position.

For rich content (e.g. an image), this function may insert the content inline, or it may add the content as an attachment (could potentially go into a completely separate view).

This function may be invoked with a clip whose MIME type is not in the list of supported types returned by getSupportedMimeTypes(). This provides the opportunity to implement custom fallback logic if desired.

Parameters
view T: The view where the content insertion was requested.

clip ClipData: The clip to insert.

source int: The trigger of the operation.

flags int: Optional flags to configure the insertion behavior. Use 0 for default behavior. See FLAG_ constants on this class for other options.

Returns
boolean Returns true if the clip was inserted.