79507853

Date: 2025-03-13 23:23:36
Score: 1
Natty:
Report link

(improved version of https://stackoverflow.com/a/45319485/4557005 )

Override the EditText's onTextContextMenuItem, in older devices without pasteAsPlainText just do as AppCompatReceiveContentHelper.maybeHandleMenuActionViaPerformReceiveContent but forcing FLAG_CONVERT_TO_PLAIN_TEXT

  @Override
  public boolean onTextContextMenuItem(int id) {
    if (id == android.R.id.paste) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        id = android.R.id.pasteAsPlainText;
      } else if (ViewCompat.getOnReceiveContentMimeTypes(this) != null) {
        // older device, manually paste as plain text
        ClipboardManager cm = (ClipboardManager) getContext().getSystemService(
          Context.CLIPBOARD_SERVICE);
        ClipData clip = (cm == null) ? null : cm.getPrimaryClip();
        if (clip != null && clip.getItemCount() > 0) {
          ContentInfoCompat payload = new ContentInfoCompat.Builder(clip, ContentInfoCompat.SOURCE_CLIPBOARD)
            .setFlags(ContentInfoCompat.FLAG_CONVERT_TO_PLAIN_TEXT)
            .build();
          ViewCompat.performReceiveContent(this, payload);
        }
        return true;
      }
    }
    return super.onTextContextMenuItem(id);
  }
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Asiel Diaz Benitez