java.lang.IllegalArgumentException: This component requires that you specify a valid android:textAppearance attribute
Asked Answered
M

12

72

I have a com.google.android.material.button.MaterialButton component in one of my layout file and I get this error when I am using the latest version of the Material Components library (com.google.android.material:material:1.0.0-alpha3):

java.lang.IllegalArgumentException: This component requires that you specify a valid android:textAppearance attribute.

It wasn't present in 1.0.0-alpha1. Is this a bug in the library or should I just specify a textAppearance attribute from now?

Mantoman answered 12/6, 2018 at 16:3 Comment(0)
C
81

Does your theme extend from Theme.MaterialComponents? More info about how to ensure all the components will work correctly can be found at https://material.io/develop/android/docs/getting-started/

Corfam answered 12/6, 2018 at 18:28 Comment(6)
I had the same problem and your solution worked fine. It turned out that my AppTheme did not inherit from Theme.MaterialComponents, but AppCompat. It used to work for 1.0.0-alpha1, but not version 3.Attenuation
Thank you! Then I think I am gonna simply specify a textAppearance attribute, because I would like to stick to a device default theme.Ric
@RolandSzép it should be using the device default even if you set the correct app theme. You should set the correct theme if you plan on using any other components from the Material Design Library. They all reference attributes set in the theme to set the correct text appearance. If you want to set a different text appearance you should probably update the text appearance attrs in the theme. More info: material.io/develop/android/theming/typographyCorfam
@Cameron Ketcham Now I get it! My problem was that when I set the Theme.MaterialComponents as my app theme (instead of android:Theme.DeviceDefault.Light), everything has changed to the default "googlish" look. (e.g. the settings activity of my app on Samsung phones wasn't the default Samsung settings look) But now I've realized that I only need to set the Theme.MaterialComponents theme on my components, that are components from the Material Design Library. And thanks for the link about the typeface, it is really helpful.Ric
For me app theme was correct but activity theme was not inherited from the material theme. Please remember activity theme should also extend Material theme.Lacto
Is there way to set theme programaticaly to MaterialDatePicker without inherit material components?Chorea
R
61

If you are using any of the MaterialComponent, then your theme must extend from the following theme - Theme.MaterialComponents.Light.DarkActionBar

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
Rectum answered 30/10, 2018 at 11:23 Comment(1)
Not only Theme.MaterialComponents.Light.DarkActionBar, every Theme.MaterialComponents.* can be used. The list of the themes: material.io/develop/android/docs/getting-started/…Kayekayla
B
48

If you want to keep using your old styles but only want to extend from 'Theme.MaterialComponents' then you can use 'Bridge'.

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorAccent</item>
        <item name="colorAccent">@color/colorPrimaryDark</item>
</style>
Bans answered 18/8, 2019 at 7:51 Comment(0)
T
10

if your theme does not extend MaterialComponents theme, and you want to keep the AppCompat theme, use a bridge theme, that would allow you to use material components keeping the AppCompat theme.

change your existing theme from:

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

to this:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
Trews answered 15/4, 2020 at 8:17 Comment(0)
R
8

Does my theme extend from Theme.MaterialComponents? Oh yes indeed, and has been since I started using any of the new Material UI stuff. If all these answers here are as unhelpful to you as they were to me, get ready: The This component requires that you specify a valid android:textAppearance attribute error can be related to an external library specifying android:theme with the same name as the theme you are using, and Android randomly deciding which one to use depending on your build.gradle. In my case the culprit was inside Mobile FFmpeg.

I started encountering this error after working for a week while the build variant was set to a different product flavor and then switching back the original one. What changed meanwhile? After thorough investigation I found the build broke after I split implementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS' in two, for each product flavor where I actually use it like this:

videoImplementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS'
mainImplementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS'

This was enough to trigger This component requires that you specify a valid android:textAppearance attribute for flavor main, while working fine for flavor video. Every main build crashed because my app's theme is named AppTheme and the Mobile FFmpeg manifest also specifies android:theme="@style/AppTheme" (which affects all versions up to 4.2.2). So I renamed my theme, but that resulted in a build error very similar to the one here: https://github.com/tanersener/mobile-ffmpeg/issues/206

    Attribute application@theme value=(@style/ScryptedTheme) from AndroidManifest.xml:37:9-45
    is also present at [com.arthenica:mobile-ffmpeg-https:4.2.LTS] AndroidManifest.xml:17:9-40 value=(@style/AppTheme).
    Suggestion: add 'tools:replace="android:theme"' to <application> element at AndroidManifest.xml:31:5-95:19 to override.

After adding said tools:replace="android:theme", everything worked again, and the original MaterialComponents error was gone.

But why is it OK for one flavor and not OK for the other? Absolutely no idea. Credit goes to Google's tendency to add the craziest bugs to "stable" releases. At least it's possible to solve very easily with some refactoring.

TL;DR

THIS is the issue: https://github.com/tanersener/mobile-ffmpeg/issues/206 Together with the fact that when two merged manifest specify different themes with the same name, Android will choose one randomly depending on the content of your build.gradle.

Solution: Add a tools:replace="android:theme" to your manifest's <application> tag and change the name of your theme.

Rigging answered 20/8, 2019 at 1:53 Comment(9)
Thanks for the input..been scratching for many days on this errror!!! But what happens when we give this attribute? which theme is selected, ours or the library's?Calycle
tools:replace will select yours, but for some reason only if the theme name is different than the library'sRigging
im a beginner, if u dint mind be asking, what shall I read such that , I shall analyse/interpret these things myself..Calycle
sorry I don't understand your question, can you reword it?Rigging
If this command replaces the library's theme with our manifest theme, wouldn't the behaviour of the library change?Calycle
Ah yes you're right, it would, but only if it actually provides any UI. Mobile FFmpeg for instance doesn't have any UI, so their android:theme declaration is redundant.Rigging
so, in case if a UI library is present, then is there a possibility that it might malfunction or crash?Calycle
Malfunction in terms of showing your theme when it should show another -> yes. Crash -> no :)Rigging
We have spent days trying to fix this issue. Finally solved it with this solution. Thanks a lot.Polyphonic
D
4

Check if your AppTheme inherits from MaterialComponents as specified here.

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
    <!-- ... -->
</style>

Remember to check all variants of styles.xml file. This was actually the issue I had.

Drive answered 11/7, 2019 at 9:23 Comment(0)
H
3

Extending Correctly? => Verify your styles!

In my case, I made a dumb auto-complete mistake:

<item name="textInputStyle">@style/Widget.MaterialComponents.Button.OutlinedButton</item>

What I really meant was TextInputLayout.OutlinedBox instead of Button.OutlinedButton:

<item name="textInputStyle">@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox</item>

Unfortunately this typo gives the same misleading error (despite extending the theme correctly):

Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).

...sharing because it took me a long time to spot the error!

Highgrade answered 17/3, 2021 at 4:27 Comment(1)
Thank you, that was my issue! I had the correct theme like Theme.MaterialComponent.Light.NoActionBar. But it still crashes. And when I added textInputStyle for my theme it works!Pell
G
2

I had the same problem, I changed my activity theme but it didnt resolved the issue. The i changed my main app theme from AppCompact to Theme.MaterialComponents

<application
    android:allowBackup="true"
    android:fullBackupContent="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme2">
<activity
        android:name=".MainActivity"
        android:label="@string/app_name"/>
</application>
Goshawk answered 10/12, 2018 at 10:6 Comment(0)
T
2

Got stucked in this error even if my theme extends Theme.MaterialComponents. I was creating Chips like this :Chip chip = new Chip(getActivity().getBasecontext(), null, R.attr.CustomChipChoice);.

The solution is to change it to Chip chip = new Chip(getActivity(), null, R.attr.CustomChipChoice);.

Hope it helps.

Tarsia answered 26/8, 2019 at 9:44 Comment(1)
Hi @Diridou ,what is CustomChipChoice here?could you show me that?Calycle
E
2

I have included this dependence first

implementation 'com.google.android.material:material:1.2.0-alpha01'

Than I have set my project style as bellow

 <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>
Earthy answered 4/11, 2019 at 13:4 Comment(0)
F
2

The problem i found was that my themes.xml was extending from a different material adress from what i was using, my dependency is 'com.google.android.material:material:1.8.0' and what i was using in the themes.xml was "Theme.MaterialComponents.DayNight.NoActionBar" but the adress to material components is now "Theme.Material3.DayNight.NoActionBar", when i changed to the new adress it began working.

Fourdrinier answered 21/4, 2023 at 17:53 Comment(0)
P
0

If you tried other answers on how to properly set parent theme for your app's theme, and you still get ...requires a value for to be set in your app theme., you may check if you have more than one place, where your app's theme is defined, as you may have separate themes.xml files for specific configuration, API versions etc.

For example:

res/values/themes.xml - default one

res/values-v23/themes.xml - for devices with API >= 23

Purdah answered 2/2, 2023 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.