Information bubble in EditText - Android

0

On websites there are several informational fields that when clicking or putting a wrong information means that a balloon comes up giving a proper information, I would like to know if it is possible to do this in java for android, if so, which lib or component I use? How do I do? I would like to do this without having to inflate a layout because it overlaps the screen. The effect I want is something like this:

    
asked by anonymous 17.03.2018 / 05:34

1 answer

1

You can use a TextWatcher in conjunction with an TextInputLayout . Your XML would look like this:

<android.support.design.widget.TextInputLayout
    android:id="@+id/inputLayoutEmail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/txtEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="email"
        android:hint="Username" />
</android.support.design.widget.TextInputLayout>

Your TextWatcher class would look like:

private class MyTextWatcher implements TextWatcher {

        private TextInputLayout inputLayout;

        private MyTextWatcher(TextInputLayout inputLayout) {
            this.inputLayout=inputLayout;
        }

        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        public void afterTextChanged(Editable editable) {
            //Fazer a validação
            if (/* Condição para ser um texto inválido */) {
                inputLayout.setError("Erro. Esse email é inválido.");
            } else {
                inputLayout.setErrorEnabled(false);
            }
        }
}

And to apply the TextWatcher to your EditText, just do:

txtEmail.addTextChangedListener(new MyTextWatcher(inputLayoutEmail));

And the result will look like this:

    
17.03.2018 / 10:15