2

Material TextInputLayout styles are not working.

I followed the documentation on material textinputlayout here: https://material.io/develop/android/components/text-input-layout/

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:hint="@string/app_name"
    app:boxStrokeColor="@android:color/white"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

With gradle:

implementation 'com.google.android.material:material:1.1.0-alpha02'

Using the style OutlinedBox, I expect to have the layout to have an outline but instead it just shows a plain TextInputLayout.

enter image description here

Gab Ledesma
  • 1,785
  • 1
  • 7
  • 13

5 Answers5

3

Change your styles.xml Theme.AppCompat.Light.NoActionBar to Theme.MaterialComponents.Light.NoActionBar

You can use below code:

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    
</resources>
blackgreen
  • 34,072
  • 23
  • 111
  • 129
Shubham
  • 31
  • 1
2

It seems that it's not working on preview, I had to run it on a device to see the real output. Thanks to @NileshRathod

I hope that android fixes this

Gab Ledesma
  • 1,785
  • 1
  • 7
  • 13
  • 1
    Please take note that the Android team doesn't own the [Material Components for Android](https://github.com/material-components/material-components-android) library. It's _developed by a core team of engineers and UX designers at Google_ (see https://github.com/material-components/material-components#material-components-shared-documentation-and-policies) – Edric Jan 08 '19 at 13:17
1

In my assumption, The problem is due to the Appcompat theme. You must change it to materialCompontents.DayNight.

Change your style.xml file to like this,

    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

And change your TextInputLayout Code as follows:

   <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#252734"
            android:hint="Enter Name" />
    </com.google.android.material.textfield.TextInputLayout>
JOSE C S
  • 31
  • 9
  • Sometimes it will not work in the ui designer of android studio. But it will surely work on real devices. – JOSE C S May 09 '21 at 03:49
0

It will work only on Material Component. It will not work on Theme.AppCompat, if any of the style uses this it will not work. So change all the Theme.AppCompat to Theme.MaterialComponents on your style.xml Like

Theme.MaterialComponents.Light.NoActionBar

shubomb
  • 672
  • 7
  • 20
0

Also, be sure you have selected the correct theme in your xml

enter image description here