Erikmitk Erikmitk - 1 year ago 157
Android Question

Paste without rich text formatting into EditText

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

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?

So I realized that the

gives me a
that contains some formatting. I can get rid of that by calling
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.

Answer Source

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 the MetricAffectingSpan and it works fine then.

public void afterTextChanged(Editable string)
    CharacterStyle[] toBeRemovedSpans = string.getSpans(0, string.length(),
    for (int index = 0; index < toBeRemovedSpans; index++)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download