The count parameter in onTextChanged method of TextWatcher is not working properly for EditText with input type textWebPassword

The count parameter in onTextChanged method of TextWatcher is not working properly for EditText with input type textWebPassword. The code inside else if(count==1) is running even if there are more than 1 characters in EditText.

public class Test extends AppCompatActivity {
private EditText ePassword;
private TextView tPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);
    tPassword = (TextView) findViewById(R.id.textView_password);
    ePassword = (EditText) findViewById(R.id.editText_password);
    ePassword.addTextChangedListener(textWatcherPassword);


}
private TextWatcher textWatcherPassword = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (count == 0) {
            // start fade out animation
            tPassword.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out));
            //Make the elements invisible
            tPassword.setVisibility(View.INVISIBLE);

        }
        else if(count==1){
            // Make fade in elements Visible first
            tPassword.setVisibility(View.VISIBLE);
            // start fade in animation
            tPassword.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in));
        }
        Log.e("Count", count + "");
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};
}

Logcat output is(for password with more than 1 character):

08-19 16:54:49.465  25167-25167/com.test.example E/Count﹕ 1
08-19 16:54:49.607  25167-25167/com.test.example E/Count﹕ 1
08-19 16:54:49.756  25167-25167/com.test.example E/Count﹕ 1
08-19 16:54:49.881  25167-25167/com.test.example E/Count﹕ 1
08-19 16:54:50.006  25167-25167/com.test.example E/Count﹕ 1
08-19 16:54:50.122  25167-25167/com.test.example E/Count﹕ 1

Answers


After some research, I got my code running by using s.length()==1 instead of count==1.

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.length() == 0) {
            // start fade out animation
            tPassword.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out));
            //Make the elements invisible
            tPassword.setVisibility(View.INVISIBLE);

        } else if (s.length() == 1) {
            // Make fade in elements Visible first
            tPassword.setVisibility(View.VISIBLE);
            // start fade in animation
            tPassword.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in));
        }
    }

Need Your Help

Creating a Binary Expression Tree given prefix notation in java

java recursion binary-tree

How should we construct the binary tree of the following "prefix" order expression?

ColdFusion: Using the same query throughout the page

mysql coldfusion cfquery

This seems like it should be easy, but man, I'm such a novice at some of this stuff. I feel like I've seen it done, so please bear with me if this is a dumb issue.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.