How to achieve centered new OutlinedBox style with TextInputLayout?
Current behavior:
<android.support.design.widget.TextInputLayout android:id="@+id/textInputLayout2" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="left" > <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="left" android:hint="hint" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/textInputLayout" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" > <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:hint="hint2" /> </android.support.design.widget.TextInputLayout>
I use com.android.support:design:28.0.0
.
Any ideas to implement centered normal looking TextInputEditText?
Android TexInputLayout extends LinearLayout. The primary use of a TextInputLayout is to act as a wrapper for EditText(or its descendant) and enable floating hint animations. Rule of Thumb : TextInputLayout should wrap TextInputEditText instead of the normal EditText.
To set a different gravity for the EditText , don't set any gravity attributes in the layout, then set the EditText 's gravity programmatically, in your code. That is, after setContentView() , use findViewById() to get the EditText , then call setGravity(Gravity. CENTER_HORIZONTAL) on it.
With the Material Components Library, starting from the version 1.2.0-alpha02
the collapsed label position is determined via the edit text gravity
.
Using something like:
<com.google.android.material.textfield.TextInputLayout
android:hint="@string/...."
...>
<com.google.android.material.textfield.TextInputEditText
android:gravity="center"
../>
</com.google.android.material.textfield.TextInputLayout>
The result is:
Centered Text inside an outlined textfield isn't really a case we support. It's not actually mentioned at all in the guidance (https://material.io/design/components/text-fields.html)
It does look like a bug that the label text is centered when center gravity is applied. The easiest way to get that fixed would be to send a pr to us with the fix on https://github.com/material-components/material-components-android. Or you can file a bug here: https://issuetracker.google.com/issues/new?component=439535.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With