Rajan Prasad
Rajan Prasad

Reputation: 1679

How to disappear the hint in TextInputEditText on receiving focus

I am using a TextInputEditText as suggested here to let the user add text up to a specific length.

However, I want that when the TextInputEditText view receives focus, the hint should disappear instead of going above the view, as shown in the aforementioned tutorial. In this answer, the solution described is for a EditText and it just makes the hint go transparent. However, for a TextInputEditText, even if I make the hint transparent on receiving focus, it will still occupy space above the view.

Is there a way to just remove it on receiving focus in the TextInputEditText, statically ? It is easy to do it through OnFocusChangeListener, but I was looking to do it at compile-time.

Upvotes: 1

Views: 5779

Answers (6)

CoolMind
CoolMind

Reputation: 28845

For those who have double hint in TextInputLayout, see TextInputLayout and EditText double hint issue. We need to set hint to TextInputLayout, not TextInputEditText.

editText.setOnFocusChangeListener { _, hasFocus ->
    inputLayout.hint = if (hasFocus) {
        ""
    } else {
        getString(R.string.some_text)
    }
}

Upvotes: 0

Riaz Ali Khan
Riaz Ali Khan

Reputation: 31

Simply add:-

try this from the following post how to make hint disappear when edittext is touched?

edittxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus)
            edittxt.setHint("");
        else
            edittxt.setHint("hint");
    }
});

Upvotes: 2

Rajneesh Kumar
Rajneesh Kumar

Reputation: 11

try to set EditText hint in java and it will automatically hide when you click on the EditText.

edtPassword.setHint(getString(R.string.YOUR_HINT));

Upvotes: 0

Vijay
Vijay

Reputation: 615

Try this if you want to disappear on focus:

textInputEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasfocus) {
            if (hasfocus) {
                textInputLayout.setHint("test");
            } else {
                textInputLayout.setHint(null);
            }
        }
    });

Try below code if you want to disappear on typing (in case you are looking for something like this):

textInputEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (TextUtils.isEmpty(textInputEditText.getText().toString())) {
                textInputLayout.setHint("test");
            } else {
                textInputLayout.setHint(null);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

Upvotes: 1

Joaquín
Joaquín

Reputation: 1136

try this from the following post how to make hint disappear when edittext is touched?

myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus)
            myEditText.setHint("");
        else
            myEditText.setHint("Your hint");
    }
});

Upvotes: 1

MarcinR
MarcinR

Reputation: 806

Try to set focus change listener on EditText and hide hint programmatically when focus is true and show again hint if focus is false and text is empty.

Upvotes: 2

Related Questions