Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change EditText textAllCaps when using TextInputLayout

Tags:

android

I am using the new TextInputLayout from the design library. I am able to get it to show and to change the color of the floating label. Unfortunately it does not change the text to uppercase.

I don't want to change my strings.xml, because I just want this label to be uppercase. I have tried changing the textAllCaps in layout, styles and programmatically but the case of EditText hint always shows exactly as the strings.xml.

Here is my XML for my TextInputLayout and EditText

<android.support.design.widget.TextInputLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android.support.design:hintTextAppearance="@style/TextInputLayout">


<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/city"
    android:textColorHint="@color/black"
    android:hint="@string/city" />

</android.support.design.widget.TextInputLayout>

And here is the style I am using for the TextInputLayout (I tried using the only "textAllCaps" (without "android:") attribute but didn't do anything for me):

<style name="TextInputLayout" parent="@android:style/TextAppearance">
 <item name="android:textAllCaps">true</item>
</style>
like image 338
Nicolas Pansardi Avatar asked May 02 '18 16:05

Nicolas Pansardi


1 Answers

The solution is almost the same with @Janvi, but it's solved by BindingAdapter which can be used in xml instead of codes:

@BindingAdapter("android:hint")
fun setTextInputLayoutHint(textInputLayout: TextInputLayout, hint: CharSequence?) {
    textInputLayout.hint = hint?.toString()?.toUpperCase()
}

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_first_name"
        android:hint="@{@string/first_name}"

enter image description here

PS:

The hint should be set on TextInputLayout, rather than the TextInputEditText or EditText. https://material.io/develop/android/components/text-input-layout/

like image 68
li2 Avatar answered Nov 11 '22 05:11

li2