1

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.

Rajan Prasad
  • 1,582
  • 1
  • 16
  • 33

6 Answers6

2

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.

MarcinR
  • 786
  • 1
  • 6
  • 16
  • It is possible to do it dynamically, yes. I wanted to see if it can be done statically at compile time. I will edit the question to reflect that. – Rajan Prasad Jul 05 '18 at 12:17
1

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");
    }
});
Joaquín
  • 1,116
  • 1
  • 10
  • 26
  • It is possible to do it dynamically, yes. I wanted to see if it can be done statically at compile time. I will edit the question to reflect that. – Rajan Prasad Jul 05 '18 at 12:16
1

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) {

        }
    });
Vijay
  • 575
  • 4
  • 14
1

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");
    }
});
tomerpacific
  • 4,704
  • 13
  • 34
  • 52
0

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));
0

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)
    }
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224