TextInputLayout The style on this component requires your app theme to be Theme AppCompat

Activityを持たないViewだけの生成で com.google.android.material.textfield.TextInputLayout を使おうと思った時に以下のエラーが発生した。

Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).

エラーの通りでテーマにTheme.AppCompatを指定しないといけないということだったけど Activityがない場合には、RootのViewGroupに設定を追加すればいいみたいだった。

xml version="1.0" encoding="utf-8" <androidxconstraintlayoutwidgetConstraintLayout xmlnsandroid="http://schemas.android.com/apk/res/android"     xmlnsapp="http://schemas.android.com/apk/res-auto"     xmlnstools="http://schemas.android.com/tools"     androidtheme="@style/Theme.AppCompat.DayNight.NoActionBar" <--------- 追加     androidlayout_width="match_parent"     androidlayout_height="match_parent">     <comgoogleandroidmaterialtextfieldTextInputLayout         androidid="@+id/textField"         androidlayout_width="match_parent"

設定したら問題なく表示された。

memo: ActivityやApplicationの追加はこちらのStackOverflowで確認できる。

stackoverflow.com

Questions : TextInputLayout Material Component Issue : The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant)

2022-08-05T00:59:57+00:00 2022-08-05T00:59:57+00:00

609

I am having a problem implementing a anycodings_android Material design with TextInputLayout, I know anycodings_android the solution is to make your activity theme anycodings_android inherits one of Material Component Theme but anycodings_android that will bring such change on most of my anycodings_android app theme and will take more time to anycodings_android refactor it. What I want is Material design anycodings_android just for this specific TextInputLayout on anycodings_android the app.

This is what I tried

<com.google.android.material.textfield.TextInputLayout android:id="@+id/firstName" style="@style/CustomInputStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="5dp" android:layout_weight="1" app:errorEnabled="true"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/fname" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/first_name" android:inputType="text|textCapWords" android:nextFocusDown="@id/lname" android:textSize="12sp" /> </com.google.android.material.textfield.TextInputLayout> <style name="CustomInputStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox"> <item name="boxStrokeWidth">2dp</item> <item name="hintTextColor">@color/colorAccent</item> <item name="boxStrokeColor">@android:color/white</item> <item name="android:colorAccent">@color/colorAccent</item> <item name="android:textColorHint">@color/colorAccent</item> </style>

Error : Caused by: anycodings_android java.lang.IllegalArgumentException: The anycodings_android style on this component requires your app anycodings_android theme to be Theme.MaterialComponents (or a anycodings_android descendant).

Can't we just do this instead of overriding anycodings_android the overall app/activity theme? All I want anycodings_android is to use it at specific Fragment.

Total Answers 3

31

Answers 1 : of TextInputLayout Material Component Issue : The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant)

I just notice that it is possible just anycodings_android-theme put the Material Components theme in the anycodings_android-theme root view of a XML layout then you can anycodings_android-theme now apply the style without any error.

In the parent/root view add this

android:theme="@style/Theme.MaterialComponents.Bridge"

Then apply the style to child view anycodings_android-theme TextInputLayout

<com.google.android.material.textfield.TextInputLayout android:id="@+id/firstName" style="@style/CustomInputStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="5dp" android:layout_weight="1" app:errorEnabled="true"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/fname" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/first_name" android:inputType="text|textCapWords" android:nextFocusDown="@id/lname" style="@style/CustomEditText" android:textSize="12sp" /> </com.google.android.material.textfield.TextInputLayout>

0

2022-08-05T00:59:57+00:00 2022-08-05T00:59:57+00:00Answer Link

mRahman

6

Answers 2 : of TextInputLayout Material Component Issue : The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant)

Problem :This occurs some times if you anycodings_android-theme want to apply a wrong style to your anycodings_android-theme "TextInputLayout".

ex: in my case I try to use the anycodings_android-theme "TextInputLayout" as Drop down list so anycodings_android-theme inside the "TextInputLayout" i put anycodings_android-theme "AutoCompleteTextView" and inside the anycodings_android-theme last i determine the style anycodings_android-theme style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu" anycodings_android-theme so it give me the above error.

Solving: change the style to the correct anycodings_android-theme one. style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu" and anycodings_android-theme it works now and the app is run anycodings_android-theme correctly.

Before: style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"

After: style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"

0

2022-08-05T00:59:57+00:00 2022-08-05T00:59:57+00:00Answer Link

raja

6

Answers 3 : of TextInputLayout Material Component Issue : The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant)

I have the same problem but when I anycodings_android-theme change the exposed dropdown style from anycodings_android-theme material3 to material components the anycodings_android-theme crash still persists but now with other anycodings_android-theme material3 components like the new anycodings_android-theme MaterialButton with

<com.google.android.material.button.MaterialButton

Furthermore it requires me to inherit anycodings_android-theme material2 (Material components) and not anycodings_android-theme material3 for my base theme ...

Update 12/05/22

I have tried your version with bridge anycodings_android-theme but now it crashes with another error. anycodings_android-theme From the documentaries an exposed anycodings_android-theme dropdown menu (m3) requires an anchor anycodings_android-theme but with your bridge it crashes because anycodings_android-theme it cannot use the anchor attribute.

I solved all my problems by checking and anycodings_android-theme changing all components to material 3 or anycodings_android-theme legacy/appcompat components. And I anycodings_android-theme styled the textinputlayout directly anycodings_android-theme inside the XML layout, now I can build anycodings_android-theme and start my app without crashes. I can anycodings_android-theme give code examples if needed ;)

0

2022-08-05T00:59:57+00:00 2022-08-05T00:59:57+00:00Answer Link

joy