Reputation: 19
hey this is my code i need some help please when the user enter his user id the edit text needs to go from red to green the code is working but only if you enter your user id and then press enter i need it to automatically change color.
editText1_id.Setontextchanged(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editText1_id.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext()," Please insert your User ID", Toast.LENGTH_SHORT).show();
}else if ((editText1_id.length() < 4)){
makeToastLogin("Please insert your User ID");
} else {
userId = editText1_id.getText().toString();
}
if (editText1_id.getText().toString().isEmpty()) {
editText1_id.setBackgroundResource(R.drawable.buttonshap_red_outline);
}else if (editText1_id.length() < 4){
editText1_id.setBackgroundResource(R.drawable.buttonshap_red_outline);
}else if (editText1_id.length() == 4) {
editText1_id.setBackgroundResource(R.drawable.insert_frame);
} else {
editText1_id.setBackgroundResource(R.drawable.insert_frame);
}
}
});
Upvotes: 0
Views: 75
Reputation: 301
Sound like you're trying to do this, except instead of 2 you want 4: Android Development: How To Use onKeyUp?
Upvotes: 0
Reputation: 1866
You cannot use an OnClickListener, you need a TextWatcher. See this answer: https://stackoverflow.com/a/11134227/3673616
Upvotes: 0
Reputation: 2731
you can put your code inside TextWatcher
like this:
editText1_id.addTextChangedListener(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) {
}
@Override
public void afterTextChanged(Editable s) {
if (editText1_id.getText().toString().isEmpty()) {
editText1_id.setBackgroundResource(R.drawable.buttonshap_red_outline);
}else if (editText1_id.length() < 4){
editText1_id.setBackgroundResource(R.drawable.buttonshap_red_outline);
}else if (editText1_id.length() == 4) {
editText1_id.setBackgroundResource(R.drawable.insert_frame);
} else {
editText1_id.setBackgroundResource(R.drawable.insert_frame);
}
}
});
as you can see you can add addTextChangedListener
to each EditText
. it has three listener that help you to notice when user start typing
Upvotes: 1