Keep TextInputLayout always focused or keep label always expanded

AndyRoid picture AndyRoid · Jan 30, 2016 · Viewed 10.2k times · Source

I was wondering if it's possible to always keep the label expanded regardless of whether or not there is text in the EditText. I looked around in the source and it is a using a ValueAnimator and a counter inside a TextWatcher to animate or not animate changes. Maybe I can set a custom TextWatcher with a custom ValueAnimator on the EditText inside the TextInputLayout?

Answer

Vesko picture Vesko · Feb 22, 2016

The current version of the TextInputLayout exists specifically to do one thing - show / hide the helper label depending on whether there's some text in the EditText or not. What you want is different behaviour, so you need a different widget than the TextInputLayout. This case is the perfect candidate to writing a custom view that will suit your needs.

That said, your idea of setting a custom TextWatcher to the EditText won't work either because TextInputLayout doesn't expose anything of it's internals that actually handle the animations - neither updateLabelVisibility(), setEditText(), the magic Handler that does the work or anything else. Of course we surely don't want to go the reflection path for a detail like this, so...

Just use MaterialEditText! It has the following property that does exactly what you want.

met_floatingLabelAlwaysShown: Always show the floating label, instead of animating it in/out. False as default.

The library is quite stable (I'm using it in two different projects myself) and has plenty of options to customise. Hope it helps!