(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);
}