Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText setError() with icon but without Popup message

I want to to have some validation for my EditText wherein I want to show "enter image description here" icon (that comes when you put editText.setError("blah blah")) but don't want the text in the popup displaying that "blah blah".

Is there any way to do it? One way is to create a custom layout which will show the image icon in the EditText. But is there any better solution?

like image 222
Rajkiran Avatar asked Jan 24 '12 10:01

Rajkiran


People also ask

How do I add an icon to EditText?

2- First thing we need to do is to look for icons that we can use them for the android edittext. Right click on res folder → New → Vector Asset . Adjust the size from (24dp x 24dp) to (18dp x 18dp), choose the icon that you want by clicking on the android icon, click “Next” button and then click “Finish”.

How do I show error in TextInputLayout?

Error Label The below xml code is from the activity_main. xml layout and has EditText fields for a default error label and a custom one. To display the error text, we'll have to call the method setError(String) on an instance of TextInputLayout in our MainActivity.

What is the difference between an EditText and a TextView?

EditText is used for user input. TextView is used to display text and is not editable by the user. TextView can be updated programatically at any time.


2 Answers

Problem solved after a lot of research and permutations- (Also thanks to @van)

Create a new class that will extend EditText something like this-

public class MyEditText extends EditText {  public MyEditText(Context context, AttributeSet attrs) {     super(context, attrs); }  @Override public void setError(CharSequence error, Drawable icon) {     setCompoundDrawables(null, null, icon, null); } } 

Use this class as a view in your xml like this-

<com.raj.poc.MyEditText     android:id="@+id/et_test"     android:layout_width="fill_parent"     android:layout_height="wrap_content"/> 

Now in the third step, just set a TextWatcher to your custom text view like this-

    et = (MyEditText) findViewById(R.id.et_test);      errorIcon = getResources().getDrawable(R.drawable.ic_error);     errorIcon.setBounds(new Rect(0, 0, errorIcon.getIntrinsicWidth(), errorIcon.getIntrinsicHeight()));        et.setError(null,errorIcon);      et.addTextChangedListener(new TextWatcher() {          @Override         public void onTextChanged(CharSequence s, int start, int before, int count) {         }          @Override         public void beforeTextChanged(CharSequence s, int start, int count,                 int after) {             // TODO Auto-generated method stub          }          @Override         public void afterTextChanged(Editable s) {             if(s.toString().length()>6){                 et.setError("", null);             }else{                 et.setError("", errorIcon);             }         }     }); 

where R.drawable.ic_error =

Keeping text null solves the problem But if we keep only null in setError(null), this won't show the validation error; it should be null along with second param.

like image 164
Rajkiran Avatar answered Sep 19 '22 18:09

Rajkiran


You dont need to create a new EditText class or change xml. The solution is very simple:

Edittext editText= (EditText) rootView.findViewById(R.id.email);  String str= editText.getText().toString();  if(str.equalsIgnoreCase("") ){                  Drawable dr = getResources().getDrawable(R.drawable.error);                                   //add an error icon to yur drawable files                 dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());                  editText.setCompoundDrawables(null,null,dr,null);              } 
like image 40
mhmttaa Avatar answered Sep 22 '22 18:09

mhmttaa