How to change background color of edittext

8.1k Views Asked by At

i've some edittext and i'm checking the string inside it entered from users. when users clicks button at the end of dialog, if some edittexts aren't filled, i change background color of it and set a text in this way:

for(int i=0;i<fields.length;i++){
            if(fields[i].getText().toString().trim().length()<=0){
                redFields(fields[i]);
            }
        }
private void redFields(EditText t){
        t.setBackgroundColor(Color.RED);
        t.setText("FIELD REQUESTED!");
    }

Now when users clicks again on a red edittext i want to change background color to white and delete text inside it ( FIELD REQUESTED ). I'm trying to do it in this way:

private void addFieldsListener(){
        for(EditText f : fields){
            final EditText ff = f;
            ff.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if(hasFocus && ff.getBackground().equals(Color.RED)){
                        ff.setBackgroundColor(Color.WHITE);
                    }
                }
            });
        }
    }

but nothing :( i try with onClickListener but it doesn't work. how can i do it? can you help me?

2

There are 2 best solutions below

0
On BEST ANSWER

Why you do not use the method SetError(...) on the EditText component ?
http://developer.android.com/reference/android/widget/TextView.html#setError%28java.lang.CharSequence%29

0
On

EditText uses a Nine Patch Drawable. You can check this link to modify the existing drawable.

Hope it helps.