Google se compromete a impulsar la igualdad racial para las comunidades afrodescendientes. Obtén información al respecto.

RichContentReceiverCompat

abstract class RichContentReceiverCompat<T : View!>
kotlin.Any
   ↳ androidx.core.widget.RichContentReceiverCompat

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 android.widget.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
static Int

Flag for onReceive requesting that the content should be converted to plain text prior to inserting.

static Int

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).

static Int

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

Public constructors

Callback for apps to implement handling for insertion of rich content.

Public methods
abstract MutableSet<String!>

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

abstract Boolean
onReceive(@NonNull view: T, @NonNull clip: ClipData, source: Int, flags: Int)

Insert the given clip.

Constants

FLAG_CONVERT_TO_PLAIN_TEXT

static val FLAG_CONVERT_TO_PLAIN_TEXT: Int

Flag for onReceive requesting that the content should be converted to plain text prior to inserting.

Value: 1 << 0

SOURCE_CLIPBOARD

static val SOURCE_CLIPBOARD: Int

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).

Value: 0

SOURCE_INPUT_METHOD

static val SOURCE_INPUT_METHOD: Int

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.

Value: 1

Public constructors

<init>

RichContentReceiverCompat()

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 android.widget.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 ...
        }
    }
  

Public methods

getSupportedMimeTypes

@NonNull abstract fun getSupportedMimeTypes(): MutableSet<String!>

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".

Return
MutableSet<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

abstract fun onReceive(
    @NonNull view: T,
    @NonNull clip: ClipData,
    source: Int,
    flags: Int
): Boolean

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)

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.
Return
Boolean Returns true if the clip was inserted.