Show Error on the tip of the Edit Text Android

前端 未结 12 2123
一生所求
一生所求 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 05:07

    Simple way to implement this thing following this method

    1st initial the EditText Field

      EditText editText = findViewById(R.id.editTextField);
    

    When you done initialization. Now time to keep the imputed value in a variable

      final String userInput = editText.getText().toString();
    

    Now Time to check the condition whether user fulfilled or not

      if (userInput.isEmpty()){
               editText.setError("This field need to fill up");
           
            }else{
             //Do what you want to do
          }
    

    Here is an example how I did with my project

      private void sendMail() {
           final String userMessage = etMessage.getText().toString();
              if (userMessage.isEmpty()) {
    
                etMessage.setError("Write to us");
    
        }else{
           Toast.makeText(this, "You write to us"+etMessage, Toast.LENGTH_SHORT).show();
          }
       }
    

    Hope it will help you.

    HappyCoding

提交回复
热议问题