1

I have these few lines of code to validate form input. However, it doesn't output anything - just a blank box. What am I missing, please?

 public void onClick(View view)
 {
    if( view.getId() == R.id.btnLogin )
    {
        TestClass.setUsername( txtUserName.getEditableText().toString() );
        TestClass.setPassword( txtPassword.getEditableText().toString() );

        if( TestClass.getUsername().toString().length() == 0 )
        {
            this.txtUserName.setError( "Incorrect input!" );
        }
        else if( TestClass.getPassword().toString().length() == 0 )
        {
         this.txtPassword.setError( "Incorrect input!" );
        }
        else
        {
               // do the login stuff here
        }
     } 
  }
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • possible duplicate of [edittext.setError() not working in android?](http://stackoverflow.com/questions/7747268/edittext-seterror-not-working-in-android) – njzk2 Mar 12 '13 at 13:02
  • possible duplicate of [Android EditText.setError() yields invisible error text](http://stackoverflow.com/questions/7273932/android-edittext-seterror-yields-invisible-error-text) – Pragnani Mar 12 '13 at 13:05

4 Answers4

3

Try this,

int ecolor = Color.RED; // whatever color you want
String estring = "Input is incorrect";  // your error message
ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
myedittext.setError(ssbuilder);

Source: Android EditText.setError() yields invisible error text

Community
  • 1
  • 1
Pragnani
  • 20,075
  • 6
  • 49
  • 74
1

Use Html object for easy and quick formatting:

this.txtUserName.setError(Html.fromHtml("<font color='red'>Incorrect input!</font>"));
mostar
  • 4,723
  • 2
  • 28
  • 45
0

Try this code:

               if (editTextName.getText().toString().length() == 0) {
                    int ecolor = getResources().getColor(R.color.blue); // whatever color you want
                    String estring = "Input is incorrect";
                    ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
                    SpannableStringBuilder builder = new SpannableStringBuilder(estring);
                    ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
                    editTextName.setError( builder );

                }
Android_coder
  • 9,953
  • 3
  • 17
  • 23
0

I just changed the application theme in my project "manifest file and error text is appearing now.

before change:

android:theme="@android:style/Theme.Light.NoTitleBar"

after change:

android:theme="@android:style/Theme.Black.NoTitleBar"
Mohsin Raza
  • 488
  • 1
  • 6
  • 12