Information balloon in Edittext - Android

Asked

Viewed 195 times

0

On websites there are several informative fields that when clicking or putting a wrong information half a balloon arises giving a proper information, I wonder if it is possible to do this in java for android, if yes, which lib or component I use? How do I do it? I would like to do this without having to inflate a layout, because then it superimposes the screen. The effect I want is something similar to that:

inserir a descrição da imagem aqui

1 answer

1


You can use a Textwatcher together with a Textinputlayout. In your XML would be:

<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 be:

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 Textwatcher to your Edittext, just do:

txtEmail.addTextChangedListener(new MyTextWatcher(inputLayoutEmail));

And the result will be similar to this:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.