1

Right now I'm building a simple form and I'm designing it so that if the user hasn't entered the necessary info before clicking the submit button the background turns red. And after if they have entered the correct info the form goes back to the way it was before.

// "if empty then set fields to red" checks
            if (firstLastName.getText().toString().equals("")) {
                firstLastName.setBackgroundColor(Color.RED);
            }
            else
                firstLastName.setBackgroundColor(Color.WHITE);
        }

The problem is that white apparently isn't what it was before, because it looks different. Is there a way to reset the form without deleting the info entered by the user?

If I'm not being clear please let me know and Ill try to elaborate.

John Leehey
  • 22,052
  • 8
  • 61
  • 88
Connor Black
  • 6,921
  • 12
  • 39
  • 70
  • 2
    Just as an alternative: you can set an error message on your EditText using `firstLastName.setError("This field needs to be filled out, yo!");` – dymmeh Jun 18 '12 at 19:50
  • 1
    is this an EditText? Try calling getBackground() on it first and storing this result temporarily so that you can reset it. More than likely, the default background is not Color.WHITE. – barbiepylon Jun 18 '12 at 19:52
  • I would suggest getting the default background color, but I don't know if that is even possible. But you can define your own theme. – Sam Jun 18 '12 at 19:52
  • This may help, particularly the last answer. http://stackoverflow.com/questions/2173936/how-to-set-background-color-of-a-view – Nate Jun 18 '12 at 19:53

1 Answers1

1

How about setting and removing a color filter instead of changing the background color:

if (firstLastName.getText().toString().equals("")) {
    // 0xFFFF0000 is red
    firstLastName.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.DARKEN);}
else {
    //Setting to null removes filter
    firstLastName.getBackground().setColorFilter(null);
}
John Leehey
  • 22,052
  • 8
  • 61
  • 88
  • 1
    +1, and some more info about why the OP's implementation is broken: The default background for any SDK widget is normally a `` or 9-patch images. Setting the color clears this complex drawable and replaces it with a single `ColorDrawable`. Once modified, there is no way to go back to the original `StateListDrawable` because those drawable ids are not exposed to the SDK. – devunwired Jun 18 '12 at 21:56