Created
October 23, 2017 16:19
-
-
Save dpkirchner/8682a21130d8a5de313793b8bdb8d16a to your computer and use it in GitHub Desktop.
how to vertically center a TextInputLayout's TextInputEditText without disabling hints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TextInputLayout adds a top margin to the TextInputEditText to reserve space for the collapsing hint. | |
That effectively breaks vertical centering. This code simply copies the newly set top margin to the | |
bottom. | |
(Note that this isn't fully tested with all languages. The hint height is set based on font | |
characteristics. If you have a font with really large descenders and small ascenders you may | |
need to make changes to this code. | |
For reference, see TextInputLayout's updateInputLayoutMargins) | |
In Kotlin, with kotlin extensions: | |
with(editTextId.parent as ViewGroup) { | |
(layoutParams as MarginLayoutParams).apply { bottomMargin = topMargin } | |
requestLayout() | |
} | |
In Java, | |
View v = findViewById(R.id.editTextId) | |
MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams() | |
lp.bottomMargin = lp.topMargin | |
v.requestLayout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This also assumes that your TextInputEditText has a null background. If it uses the default background you'll need to figure out how to deal with padding issues. This code probably won't be useful.