I mean, in which function should i use a text watcher?
I’ve tried to use it in the onResume() function, but that didn’t work.
I’m trying to check one of each characters entered in my EditText box while the user is typing. More specifically, I gave the user a riddle and the answer is ‘SOS’. The answer goes in the EditText box and I want to check each character right after its input (I’ve added a max length attribute to the edittext box so it won’t exceed 3). When I find a wrong character, i want to set an X icon image visible, and the opposite if the character is correct.
Here is my TextWatcher code snippet:
final EditText editText=(EditText)findViewById(R.id.editTxt_three);
editText.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void afterTextChanged(Editable s)
{
for(int i=0;i<editText.length();i++)
{
if(i==0)
{
img=(ImageView)findViewById(R.id.icon1);
if(s.charAt(i)!='s' || s.charAt(i)!='S')
img.setVisibility(1);
else
img.setVisibility(0);
}
if(i==1)
{
img=(ImageView)findViewById(R.id.icon2);
if(s.charAt(i)!='o' || s.charAt(i)!='O')
img.setVisibility(1);
else
img.setVisibility(0);
}
if(i==2)
{
img=(ImageView)findViewById(R.id.icon3);
if(s.charAt(i)!='s' || s.charAt(i)!='S')
img.setVisibility(1);
else
img.setVisibility(0);
}
}
}
});
In short, where should i use this code?
Feel free to simplify my code if you think of something 🙂
You can use textwatcher in onCreate, the codes below helps you i think .