Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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() {

    publicvoidonTextChanged(CharSequence s, int start, int before,
            int count) {
            if(!s.equals("") ) { 
                //do your work here 
            }
    }



    publicvoidbeforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    publicvoidafterTextChanged(Editable s) {

    }
});

Also check this link for more idea,

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

Solution 2:

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() {

    publicvoidonTextChanged(CharSequence s, int start, int before,
            int count) {
        if (s.length() > 0)
                { //do your work here }
        }

    }

    publicvoidbeforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    publicvoidafterTextChanged(Editable s) {

    }
});

Solution 3:

I used this and it's correct:

publicvoid afterTextChanged(Editable arg0) {
    String inputs = input.getText().toString();
    Integer index=0;
    if(!inputs.equals("")) 
        index=Integer.valueOf(inputs);

}

Solution 4:

I think you need to check your editText value is empty or not first. Something like this:

String textValue;
textValue = edittext().getText().toString());
Log.v("","Value is " + textValue);
if(textValue != ""){
   // Call Text Change Listener Here
}else{
   // Throw error message or something
}

Hope it's help.

Solution 5:

Added Kotlin implementation for future reference

 webUrl.editText!!.addTextChangedListener(object : TextWatcher {
            overridefunafterTextChanged(s: Editable?) {
            }

            overridefunbeforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            overridefunonTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                var urlToload: String
                if (s ==null || s.isEmpty()) {
                    urlToload = UniversalWebViewFragment.YOUTUBE_SERACH_URL + "hd trailers"
                } else {
                    urlToload = UniversalWebViewFragment.GOOGLE_SERACH_URL + s.toString()
                }

                loadURL(urlToload)
            }

        })

Post a Comment for "How To Apply The Textchange Event On Edittext"