Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Apply the Textchange event on EditText

I developed one simple app, like subtraction, addition. In this app I use three EditTexts, one for answer and other two for question. I want to calculate the answer of question on text change event. But when I apply the text change event on both of this the event occur but not properly work. Because when I enter in the text in first EditText of question the event occur but it throws this exception:

07-03 16:39:48.844: E/EduApp Log :=>(12537): Error In Text change Event java.lang.NumberFormatException: unable to parse '' as integer 

What do I do? I use the TextWatcher for text change event.

txtOne.addTextChangedListener(this); txtTwo.addTextChangedListener(this);  public void afterTextChanged(Editable s) {}  public void beforeTextChanged(CharSequence s, int start, int count, int after) {}  public void onTextChanged(CharSequence s, int start, int before, int count) {}   
like image 670
Zala Janaksinh Avatar asked Jul 03 '12 11:07

Zala Janaksinh


People also ask

How do I know if EditText has changed?

Implement a TextWatcher. It gives you three methods, beforeTextChanged , onTextChanged , and afterTextChanged . The last method shouldn't be called until something changes anyway, so that's a good thing to use for it.

How do you implement TextWatcher in Kotlin?

This example demonstrates how to use the TextWatcher class in kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

How do I turn an editable into a string?

Simply use toString() on the Editable instance to get String.


2 Answers

I think you are receiving empty String " " which is causing this problem. Make sure you get a non-empty String from your EditText.

Consider your EditText doesn't have any value typed in, and you are trying to get its value and convert into int you will run into this kind of problem.

edittext.addTextChangedListener(new TextWatcher() {      public void onTextChanged(CharSequence s, int start, int before,             int count) {             if(!s.equals("") ) {                  //do your work here              }     }        public void beforeTextChanged(CharSequence s, int start, int count,             int after) {      }      public void afterTextChanged(Editable s) {      } }); 

Also check this link for more idea,

https://stackoverflow.com/a/3377648/603744

like image 60
Andro Selva Avatar answered Sep 16 '22 11:09

Andro Selva


i think the best in case the edittext type is number... use the (length function) of parameter instead of (equle() function) ex:

edittext.addTextChangedListener(new TextWatcher() {      public void onTextChanged(CharSequence s, int start, int before,             int count) {         if (s.length() > 0)                 { //do your work here }         }      }      public void beforeTextChanged(CharSequence s, int start, int count,             int after) {      }      public void afterTextChanged(Editable s) {      } }); 
like image 27
user6757534 Avatar answered Sep 18 '22 11:09

user6757534