Android setError("error") not working in Textview

Bhavesh Hirpara picture Bhavesh Hirpara · Nov 22, 2012 · Viewed 31.5k times · Source

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

Answer

nagoya0 picture nagoya0 · Apr 11, 2013

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");