Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android setError("error") not working in Textview

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

like image 789
Bhavesh Hirpara Avatar asked Nov 22 '12 08:11

Bhavesh Hirpara


People also ask

How to set error in TextView in android?

Default TextView is NOT focusable. So, you need to set android:focusable="true" and android:focusableInTouchMode="true". And no need to set setSelected(true). I prefer this solution.

How do I show error in TextInputLayout?

You can use the TextInputLayout to display error messages according to the material design guidelines using the setError and setErrorEnabled methods. In order to show the error below the EditText use: TextInputLayout til = (TextInputLayout) findViewById(R. id.

How do you show error messages on android?

Solution: We can display error message in EditText by using setError() method. <?


2 Answers

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"); 
like image 137
nagoya0 Avatar answered Oct 14 '22 10:10

nagoya0


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" 
like image 35
android developer Avatar answered Oct 14 '22 10:10

android developer