1

I have an edit text which I validate. If the data entered does not corespond to the format, I set the background to red, when it coresponds I set it back to light gray, but the rectangle disappears. I was wondering if I could reset it's properties to their orignal values when the data entered has the correct format. This is what i am doing now

EditText name = (EditText) findViewById(R.id.Name);
if (name.getText().length() < 1)
{
    error = true;
    unit.setBackgroundColor(Color.RED);
}
else
{
    //instead of this line reset edittext properties
    unit.setBackgroundColor(Color.LTGRAY);
}
Liviu
  • 38
  • 9

5 Answers5

1

You can use PorterDuff instead - it has a clear method: http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html.

To set the color:

name.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);

To remove the color:

name.getBackground().clearColorFilter();
Gingerade
  • 134
  • 1
  • 3
0

You can try:

editText.setText("");
SamDroid
  • 601
  • 1
  • 10
  • 28
0

I'd recommend using the setError() method, which displays an error message above the EditText and removes it automatically when the contents of the EditText change. This is a standard way to show the user that the validation has failed.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • i am required to set the background to red, and then revert to the original edittext, my problem is that the border disappears when changing back to the original background color – Liviu May 30 '13 at 08:12
0

You are changing the EditText background so you are overriding android's default background.
you need to manually change it to some other background.
you can save it by Using the getBackground() method in EditText.

Royi
  • 745
  • 8
  • 22
0

You can save the status of the editText before to change it, and restore when you need:

private Drawable originalDrawable;

EditText name = (EditText) findViewById(R.id.Name);
if (name.getText().length() < 1) {
    if (originalDrawable == null) {
        originalDrawable = unit.getBackground();
    }
    error = true;
    unit.setBackgroundColor(Color.RED);
}
else {
    //reset editText 
    text.setBackgroundDrawable(originalDrawable);
}
Roberto
  • 4,524
  • 1
  • 38
  • 30