33

We can set error in Edittext successfully but failed to set in textview. is there any problem?? i tried

((TextView) findViewById(R.id.df)).requestFocus();
((TextView) findViewById(R.id.df)).setSelected(true);
((TextView) findViewById(R.id.df)).setError("akjshbd");

but i am not getting popup for error.

Textview Error

Bhavesh Hirpara
  • 22,255
  • 15
  • 63
  • 104
  • @Anis, this is written in http://developer.android.com/reference/android/widget/TextView.html, that setError(CharSequence error): Sets the right-hand compound drawable of the TextView to the "error" icon and sets an error message that will be displayed in a popup when the TextView has focus. – MysticMagicϡ Nov 22 '12 at 08:56

4 Answers4

100

Default TextView is NOT focusable. So, you need to set android:focusable="true" and android:focusableInTouchMode="true".

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:text="@string/hello_world" />

And no need to set setSelected(true).

((TextView) findViewById(R.id.df)).requestFocus();
((TextView) findViewById(R.id.df)).setError("akjshbd");
nagoya0
  • 2,768
  • 2
  • 23
  • 28
42

Actually , you can use the setError for the textView and show its popup .

You just need to use the same style as of the EditText .

Simply add the next attribute for the textView in the xml :

style="@android:style/Widget.EditText"
android developer
  • 114,585
  • 152
  • 739
  • 1,270
14

This is the only you need to get expected setError behaviour on the TextView

android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
cesards
  • 15,882
  • 11
  • 70
  • 65
5

Snippet: you have to requestFocus(); a view to show the error.

    // Check for a valid email address.
if (TextUtils.isEmpty(mEmail)) {
    mEmailView.setError(getString(R.string.error_field_required));
    focusView = mEmailView;
    cancel = true;
} else if (!mEmail.contains("@")) {
    mEmailView.setError(getString(R.string.error_invalid_email));
    focusView = mEmailView;
    cancel = true;
}

if (cancel) {
    // There was an error; don't attempt login and focus the first
    // form field with an error.
    focusView.requestFocus();
} else {
    // Show a progress spinner, and kick off a background task to
    // perform the user login attempt.
    // showProgress(true);
    // mAuthTask = new UserLoginTask();
    // mAuthTask.execute((Void) null);
    ParseUser.logInInBackground(mEmail, mPassword, new LogInCallback() {

    @Override
    public void done(ParseUser user, ParseException e) {
        finishAndStartCardActivity();
    }
    });
}
Philippe David
  • 8,814
  • 3
  • 24
  • 35