Skip to content

Instantly share code, notes, and snippets.

@dpkirchner
Created October 23, 2017 16:19
Show Gist options
  • Save dpkirchner/8682a21130d8a5de313793b8bdb8d16a to your computer and use it in GitHub Desktop.
Save dpkirchner/8682a21130d8a5de313793b8bdb8d16a to your computer and use it in GitHub Desktop.
how to vertically center a TextInputLayout's TextInputEditText without disabling hints
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()
@dpkirchner
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment