Search code examples
androidvalidationpopupandroid-edittext

How to display input errors in popup?


I want to show all my validation error's of EdiText fields in a popup as shown in below image:

Error alert in popup

As far as I know Android has drawables:

1) popup_inline_error.9.png

popup_inline_error.9.png

2) popup_inline_error_above.9.png

popup_inline_error_above.9

3) indicator_input_error.png

indicator_input_error.png

I am able to display the red error indicator inside the right side of the EditText by using:

Drawable err_indiactor = getResources().getDrawable(R.drawable.indicator_input_error);
mEdiText.setCompoundDrawablesWithIntrinsicBounds(null, null, err_indiactor, null);

Now also i want to display the error message as shown is the first image but it seems I am not getting any idea about this, though I think it should be a Custom Toast.


Solution

  • Try the following:

    final EditText editText = (EditText) findViewById(R.id.edit);
    
    editText.setImeActionLabel("", EditorInfo.IME_ACTION_NEXT);
    
    editText.setOnEditorActionListener(new OnEditorActionListener() {
    
        @Override
        public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                if (editText.getText().toString().isBlank())
                    editText.setError("Please enter something.");
                else
                    Toast.makeText(getApplicationContext(), "Entered: " + editText.getText().toString(), Toast.LENGTH_SHORT).show();
            }
            return false;
        }
    });