Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As the earlier answer is solution for my problem but I have tried a different approach to use a custom Drawable image instead of default <code>indicator_input_error</code> image.</p> <h2>Default Drawable</h2> <p><img src="https://i.stack.imgur.com/glku4.png" alt="Default Drawable"></p> <h2>Custom Drawable</h2> <p><img src="https://i.stack.imgur.com/dBQZu.png" alt="Custom Drawable"></p> <p>So, I have just created two <code>EditText</code> in my layout xml file and then implemented some <code>Listener</code> in Java code on that <code>EditText</code>.</p> <h2>main.xml</h2> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="20dip" android:background="#222222"&gt; &lt;EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username" android:id="@+id/etUsername" android:singleLine="true" android:imeActionLabel="Next"&gt;&lt;/EditText&gt; &lt;EditText android:layout_width="match_parent" android:inputType="textPassword" android:layout_height="wrap_content" android:hint="Password" android:id="@+id/etPassword" android:singleLine="true" android:imeActionLabel="Next"&gt;&lt;/EditText&gt; &lt;/LinearLayout&gt; </code></pre> <h2>EditTextValidator.java</h2> <pre><code>import java.util.regex.Pattern; import android.app.Activity; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.widget.EditText; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; public class EditTextValidator extends Activity { private EditText mUsername, mPassword; private Drawable error_indicator; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Setting custom drawable instead of red error indicator, error_indicator = getResources().getDrawable(R.drawable.emo_im_yelling); int left = 0; int top = 0; int right = error_indicator.getIntrinsicHeight(); int bottom = error_indicator.getIntrinsicWidth(); error_indicator.setBounds(new Rect(left, top, right, bottom)); mUsername = (EditText) findViewById(R.id.etUsername); mPassword = (EditText) findViewById(R.id.etPassword); // Called when user type in EditText mUsername.addTextChangedListener(new InputValidator(mUsername)); mPassword.addTextChangedListener(new InputValidator(mPassword)); // Called when an action is performed on the EditText mUsername.setOnEditorActionListener(new EmptyTextListener(mUsername)); mPassword.setOnEditorActionListener(new EmptyTextListener(mPassword)); } private class InputValidator implements TextWatcher { private EditText et; private InputValidator(EditText editText) { this.et = editText; } @Override public void afterTextChanged(Editable s) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.length() != 0) { switch (et.getId()) { case R.id.etUsername: { if (!Pattern.matches("^[a-z]{1,16}$", s)) { et.setError("Oops! Username must have only a-z"); } } break; case R.id.etPassword: { if (!Pattern.matches("^[a-zA-Z]{1,16}$", s)) { et.setError("Oops! Password must have only a-z and A-Z"); } } break; } } } } private class EmptyTextListener implements OnEditorActionListener { private EditText et; public EmptyTextListener(EditText editText) { this.et = editText; } @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_NEXT) { // Called when user press Next button on the soft keyboard if (et.getText().toString().equals("")) et.setError("Oops! empty.", error_indicator); } return false; } } } </code></pre> <p>Now I have tested it like:</p> <blockquote> <p>For empty EditText validations :</p> </blockquote> <p>Suppose user click on the <code>Username</code> field then Softkeybord opens and if user press <code>Next</code> key then the user will be focused to the <code>Password</code> field and <code>Username</code> field remains empty then the error will be shown like as given in below images:</p> <p><img src="https://i.stack.imgur.com/BUgHX.png" alt="Empty text"> <img src="https://i.stack.imgur.com/zkZiu.png" alt="Empty text"></p> <blockquote> <p>For wrong input validations :</p> </blockquote> <p>1) I type the text <code>vikaS</code> in Username field then error will be like as given in below image :</p> <p><img src="https://i.stack.imgur.com/ahU4l.png" alt="Wrong username"></p> <p>2) I type the text <code>Password1</code> in password field then error will be like as given in below image :</p> <p><img src="https://i.stack.imgur.com/zODzV.png" alt="wrong password"></p> <h2>Note:</h2> <p>Here I have used custom drawable only in case of when user left the EditText field blank and press Next key on key board but you can use it in any case. Only you need to supply <code>Drawable</code> object in <code>setError()</code> method.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload