Search code examples
androidandroid-activityandroid-edittextpassword-protectiontextwatcher

Define a value to EditText


I'm trying to make an Activity with an EditText that can only accept a value that I defined programatically in advanced -kind off like a password, like a word or a code number- so that the user can't access the next Activity if he dosen't enter the right password.

Does anyone have an idea on how to make something like that ? Do I have to use a TextWatcher ? If yes, how ?

Thanks!


Solution

  • Use this.

    If (edittext.getText().toString()==myPreProgrammedString){
       start next activity
    }
    else{
       show warning wrong password
    }
    

    Usually you would put something like this in a onClick method of a login button. But I use something similar in textwatchers afterTextChanged method to check if the entered text is in a list and then enable the OK button.

    BTW: hardcoding passwords in an app is never good practice.