Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText with a Fixed Suffix

I need an EditText with a fixed Suffix. I'm using a TextWatcher to catch the onTextChanged Event, but if I change the Text again by adding my suffix it causes a stackoverflow exception caused by an infinite recursion.


 msgtextview.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {

               //Doing some other stuff

               msgtextview.setText("Changed Text") //This cause infinite recursion

            }               
        }

Here's the code.

like image 368
Martino Avatar asked Nov 15 '22 06:11

Martino


1 Answers

I assume you're having a stack overflow because when you programmatically add the suffix, it triggers an onTextChanged event.

Have you thought about setting a flag right before you set it programmatically so the next time it's called you can know not to add the suffix?

The alternative is to only add the suffix after the user submits your form.

like image 182
Brandon O'Rourke Avatar answered Jan 02 '23 18:01

Brandon O'Rourke