Skip to content

Conversation

@zx2c4
Copy link

@zx2c4 zx2c4 commented Mar 28, 2020

Everybody wants to have a label for a edit text box. But some people
also want to have some default gray value shown when the box doesn't
have anything put in there. This used to be called the "hint". But now,
the "hint" is actually used as a label. Sort of:

If the hint is set on the TextInputEditText, it operates like a normal
hint, being visible when there's no text inside. If a hint is set on the
TextInputLayout, however, it operates like a label, unless the
TextInputEditText has no text, in which case, it operates like a hint.
The result is that one winds up with two hints, one on top of the other
in an illegible mess:

image

I'm currently hacking around this by subclassing TextInputEditText with
this horror:

    @Override
    override fun getText(): Editable? {
        val text = super.getText()
        if (!text.isNullOrEmpty())
            return text
        /* We want this expression in TextInputLayout.java to be true if there's a hint set:
         *        final boolean hasText = editText != null && !TextUtils.isEmpty(editText.getText());
         * But for everyone else it should return the real value, so we check the caller.
         */
        if (!hint.isNullOrEmpty() && Thread.currentThread().stackTrace[3].className == TextInputLayout::class.qualifiedName)
            return SpannableStringBuilder(hint)
        return text
    }

It doesn't get worse than that. This commit fixes the issue by avoiding
the expanded hint in the case that the enclosing EditText already has a
hint. It works:

image

Everybody wants to have a label for a edit text box. But some people
also want to have some default gray value shown when the box doesn't
have anything put in there. This used to be called the "hint". But now,
the "hint" is actually used as a label. Sort of:

If the hint is set on the TextInputEditText, it operates like a normal
hint, being visible when there's no text inside. If a hint is set on the
TextInputLayout, however, it operates like a label, unless the
TextInputEditText has no text, in which case, it operates like a hint.
The result is that one winds up with two hints, one on top of the other
in an illegible mess.

I'm currently hacking around this by subclassing TextInputEditText with
this horror:

    @OverRide
    override fun getText(): Editable? {
        val text = super.getText()
        if (!text.isNullOrEmpty())
            return text
        /* We want this expression in TextInputLayout.java to be true if there's a hint set:
         *        final boolean hasText = editText != null && !TextUtils.isEmpty(editText.getText());
         * But for everyone else it should return the real value, so we check the caller.
         */
        if (!hint.isNullOrEmpty() && Thread.currentThread().stackTrace[3].className == TextInputLayout::class.qualifiedName)
            return SpannableStringBuilder(hint)
        return text
    }

It doesn't get worse than that. This commit fixes the issue by avoiding
the expanded hint in the case that the enclosing EditText already has a
hint.
@leticiarossi
Copy link
Contributor

I think this is similar to #810

Can't you just use the hint on the TextInputLayout along with the placeholderText (#810 (comment))?

@zx2c4
Copy link
Author

zx2c4 commented Mar 31, 2020

android:hint is a standard attribute. This patch makes the desired behavior trivially supported by standard attributes. Why wouldn't you want that?

@zx2c4
Copy link
Author

zx2c4 commented Apr 2, 2020

And actually, @leticiarossi, the behavior is different. placeholderText only shows when the control is selected, whereas a proper hint (this PR) always shows. Please merge this bug fix.

@leticiarossi
Copy link
Contributor

The expected usage is to have only one hint which is set in the TextInputLayout instead of on the edit text, we don't want to change the internal behavior of how that currently works.

It seems like what you want is to have the hint always in its collapsed state (something like in this PR #1028) and/or have the placeholder text always showing, which is something we can consider. We'd have to check with our design team as it's not something currently in the spec.

@zx2c4
Copy link
Author

zx2c4 commented Apr 2, 2020

The thing is that what you just mentioned is trivially possible with this commit. This commit doesn't change any existing behavior at all. It only fixes existing brokenness (see https://user-images.githubusercontent.com/10643/77832716-b097da00-70fd-11ea-809e-3c6928c66b62.png), and replaces it with something that the user clearly meant with the XML (see https://user-images.githubusercontent.com/10643/77832735-c86f5e00-70fd-11ea-8cdd-3b1b9eb86c5b.png). Fixing that bug doesn't impact any other behavior and doesn't clash with your plans for anything else. This is simply a bug fix, not something that needs design review.

@ivanbartsov
Copy link

For what it's worth, I also vote for this fix to be merged, kudos @zx2c4 .

@C2H6O
Copy link

C2H6O commented May 13, 2020

Can we get this merged in? Sometimes you want to have the title of the view to be different from the hint and the hint to be shown at all times.

Copy link

@aljohnston112 aljohnston112 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One line change for fixing the bug where the layout hint overwrites the edit hint.

@zx2c4
Copy link
Author

zx2c4 commented Jul 17, 2020

Indeed, months later and this is still not merged. Wondering what's up?

@ymarian ymarian closed this in 22973ff Sep 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants