Show Error on the tip of the Edit Text Android

前端 未结 12 2124
一生所求
一生所求 2020-12-01 04:30

I want to show error if the user enters blank value in the edittext.But i am not getting the way how could i do this .This is how i want like this:

12条回答
  •  一个人的身影
    2020-12-01 04:44

    You can show error as PopUp of EditText

    if (editText.getText().toString().trim().equalsIgnoreCase("")) {
          editText.setError("This field can not be blank");
    }
    

    and that will be look a like as follows

    enter image description here

    firstName.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) {
        }
    
        @Override
        public void afterTextChanged(Editable s)  {
            if (firstName.getText().toString().length <= 0) {
                firstName.setError("Enter FirstName");
            } else {
                firstName.setError(null);
            }
        }
     });
    

提交回复
热议问题