If I copy/paste text from Chrome for Android into my EditText view it gets messed up, apparently due to rich text formatting.
The font size is totally messed up and not that big http://erikmi.tk/WaeG/image.png
Is there a way to tell the EditText view to ignore rich text formatting? Or can I catch the paste event and remove it before it gets set? How would I do that?
UPDATE:
So I realized that the editText.getText()
gives me a SpannableString
that contains some formatting. I can get rid of that by calling .clearSpans();
on it. BUT I cannot do anything like that in editText.addTextChangedListener(new TextWatcher() { … }
because it gets terribly slow and the UI only updates when I leave the editText view.
The problem with
clearSpans()
was that it removed too much and the editText behaves weird thereafter. By following the approach in this answer I only remove theMetricAffectingSpan
and it works fine then.This simple copy and paste should give you text without formatting:
Erik's answer above removes few formatting, but not all. Hence I used:
CharacterStyle[] toBeRemovedSpans = string.getSpans(0, string.length(), CharacterStyle.class); to remove all formatting.
A perfect and easy way: Override the
EditText
'sonTextContextMenuItem
and intercept theandroid.R.id.paste
to beandroid.R.id.pasteAsPlainText
And the copyToClipBoard: