Search code examples
androidtextviewandroid-edittexthint

Why does Android have hint on TextView instead of EditText?


Hints are the text that appears in text fields (Android XML calls them EditText) before any text is entered, and disappears when the user adds any characters to the field. A basic example would be a field that looks like [First name] to hint to the user that they should type in their first name into the text field, and after they enter "B" it would look like [B ].

Yet, hints are found on the TextView in Android, rather than the EditText.

EditText inherits from TextView, but why? Why not just have hints exist for EditText? Can a TextView even use the hint?


Solution

  • Can a TextView even use the hint?

    Yes. And there are use-case for it, especially when loading data dynamically and you may want to have different texts and text styling for those states.

    enter image description here

    In the screenshot above, you can see that you can set a separate textColor and textColorHint for those states, however, textStyle applies to both.

    EditText inherits from TextView, but why?

    Thats a question for Google, but it makes sense, if you're going to have the same drawing logic for text for both attributes, why reinvent the wheel? Also, note that hint would constitute as textual drawing, which is happening in TextView, so it makes sense to do it there.