Show confirmation message

I have an XML layout file containing EditTextand Button. I would like to display the following validation message to provide feedback to the user:

You must enter 4 numbers

What is the best way to do this?

+5
source share
2 answers

In my experience, this is the best way:

EditText yourEditText;

// when you detect an error:
yourEditText.setError("Input must be 4 digits and numeric");

Result:

enter image description here

Also, if the input should be numeric, use android:inputType="numberSigned"in the definition EditText. Thus, the device will not allow the user to enter non-numeric values; even better, a special keyboard will be shown for this:

enter image description here

+13
source

EditText xml android: numeric, IME android: maxLength= "4", 4 . android: onClick , .

public void onClick(View v) {
    if(mEditText.length() != 4) { // check if 4 digits
        Toast.makeText(this, "Input must be 4 digits and numeric", Toast.LENGTH_SHORT).show();
    }
}
0

All Articles