Beta
Beta

Reputation: 345

Android - TextWatcher is invoked once for each character in pasted text

I've added a TextWatcher to an EditText and am listening for changes in the text via the onTextChanged(CharSequence s, int start, int before, int count) method. When I paste text that has say 10 characters, into this EditText, onTextChanged() gets called 10 times, once for each character in the text I pasted, from left to right. I want onTextChanged() to be called only once after all 10 characters have been pasted into the EditText. I'm sure this should be possible, because otherwise what's the point in having the "count" param if it's always going to be 1?

Upvotes: 1

Views: 2407

Answers (2)

ben or
ben or

Reputation: 1

Try using afterTextChanged it will only get one call

Upvotes: -1

Matthew
Matthew

Reputation: 44919

count won't always be 1: for instance, if you select and delete a block of text or if you choose an autocomplete option.

In any case, the details of whether pasting happens in one chunk or one character at a time is an implementation detail, and if you rely on either behavior it's likely your app will break in the future.

Upvotes: 2

Related Questions