I want to to have some validation for my EditText wherein I want to show "" 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?
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”.
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.
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.
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.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With