0

How to restrict HTML content while pasting some text from a website to our EditText?.

Example I copy some text from a website, when I paste the text in my EditText the HTML font and text size also comes along. What i want is to restrict any HTML content while I paste the text. Tried several methods using TextWatcher and InputFilters, but none of them are working. Whatsapp is handling it somehow. Please help.

Edit: The string can be converted to non-HTML but how to make changes in EditText?

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23
  • @Nj I do not think this question is duplicate of tagged post. Please read once before marking duplicate to any answer. – Pankaj Kumar Jul 19 '18 at 13:05

1 Answers1

0

This is likely a Rich Text Formatting issue.

According to this question you can add a TextWatcher to your EditText and use this method:

public void afterTextChanged(Editable string)
{
    CharacterStyle[] toBeRemovedSpans = string.getSpans(0, string.length(),
                                                MetricAffectingSpan.class);
    for (int index = 0; index < toBeRemovedSpans; index++)
        string.removeSpan(toBeRemovedSpans[index]);
    }
}

Reference:

https://stackoverflow.com/a/24777290/5457878

gi097
  • 7,313
  • 3
  • 27
  • 49