I'm getting a ThemeEnforcement error with the following. warnings.
Caused by: android.view.InflateException: Binary XML file line #9 in com.yourproject:layout/app_bar_main: Error inflating class com.google.android.material.appbar.AppBarLayout
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
Typical solutions suggest that i'm not applying Theme.AppCompat to my app theme. except i Am.
<style name="Theme.TestApp" parent="Theme.AppCompat">
</style>
Also, other solutions suggest this is caused by not including the appropriate material libraries, which are included and i have previously been able to successfully compile and run the project.
The project compiles with no errors but in some instances the app will crash out immediatly but in other instances (where i have disabled some components) the crash will happen when layout out other views.
In the latter crashes, the error may have issues inflating a standard Button or TextField.
TLDR not setting custom attr values which are used in layouts causes this error.
Part of the problem was that the error messaging was specifically stating that the problem was i wasn't applying the Theme.AppCompat, but in fact I was.
What I wasn't aware of however was that a nav_header_main.xml file was applying the style AppMenuTitle to a Textfield.
This Style was consuming a custom attribute called my.custom.attr. The issue was caused by the fact that this attr's value had not be set in the theme. So it was essentially null.
<style name="AppMenuTitle" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textColor">?my.custom.attr</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">18sp</item>
</style>
Now because these attr values were being used in a number of places, I also got errors for inflating your standard Button. And this got really confusing because the buttons were just straight up buttons, with no styling applied directly.
In stead, buttons where obtaining there styles from themes applied to parent containers, whos styles had the buttonStyle properties set.
These buttons were using custom drawables. Which guess what, were also using custom attribute values, which guess what....where also not set in the theme.
Ensuring these custom attrs had values, resolved the error.
<style name="Theme.TestApp" parent="Theme.AppCompat">
<item name="my.custom.attr.used.in.textstyle">#color/black</item>
<item name="my.custom.attr.used.in.drawable">#color/black</item>
</style>
Related
My Android app is crashing because of the following error:
Fatal Exception: android.view.InflateException Binary XML file line
#100 in com.example.myapp:layout/floating_view: Binary XML file line #100 in com.example.myapp:layout/floating_view: Error inflating class <unknown>
The related section of the xml file floating_view.xml, where line #100 is the textColor:
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="?attr/textColor" />
Here it's obviously about the attribute and styles. However, my attribute and styles are defined correctly. As you can see below in my styles.xml, the attribute textColor has a corresponding value in each theme.
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="textColor">#20282D</item>
</style>
<style name="AppThemeLight" parent="AppTheme">
<item name="textColor">#20282D</item>
</style>
<style name="AppThemeDark" parent="AppTheme">
<item name="textColor">#FFFFFF</item>
</style>
</resources>
However as I dig further in the error log, it has traces of some other theme names:
Caused by java.lang.UnsupportedOperationException Failed to resolve
attribute at index 4: TypedValue{t=0x2/d=0x7f040289 a=-1},
theme={InheritanceMap=[id=0x103013fandroid:style/Theme.DeviceDefault.Light.DarkActionBar,
id=0x1030238android:style/Theme.Material.Light.DarkActionBar,
id=0x1030237android:style/Theme.Material.Light,
id=0x103000candroid:style/Theme.Light,
id=0x1030005android:style/Theme],
Themes=[android:style/Theme.DeviceDefault.Light.DarkActionBar,
forced]}
As you can see above, the log from the affected device mentions some themes like Theme.DeviceDefault.Light.DarkActionBar, android:style/Theme.Material.Light etc.. I initially thought it's because my textColor is not defined in those themes. But I have added only 3 themes, and how can I add my custom attribute inside Android's own themes? Do you think the error is because of that, or what else could this error be about?
Also, retrieving the colors in Java code by providing a default value could be an option. However I have so many colors used in my layouts (only in XML) and moving them all to Java is not really ideal in this case.
Any help is appreciated, thanks!
As a beginner in Android development, I'm currently creating a calculator application.
The purpose
I would like to create a specific style applicable to several elements has we can do with CSS class in web development.
The Style will must be applicable to the nine numerics buttons of the calculator (1 to 9).
To avoid repeating the same attributes 9 times (once time per "Button" element), I've created a "Style" block where I've put all the attributes commons of each button.
I've added this <style> in a XML resource file named "styles.xml", the content of this file is well loaded by the application because don't have any problems with the properties "dimen" and "colors" declared in this file.
Here's the code:
<!-- Style of the calculator numerics buttons -->
<dimen name="calculatorNumberWidth">60dp</dimen>
<dimen name="calculatorNumberHeight">60dp</dimen>
<style name="calculatorNumberStyle" parent="TextAppearance.AppCompat">
<item name="android:layout_width">90dp</item>
<item name="android:layout_height">90dp</item>
<item name="android:gravity">center</item>
<item name="backgroundTint">#F1FAEE</item>
<item name="android:textColor">#000000</item>
<item name="android:textSize">36dp</item>
<item name="layout_constraintBottom_toBottomOf">parent</item>
<item name="layout_constraintRight_toRightOf">parent</item>
<item name="layout_constraintTop_toTopOf">parent</item>
</style>
My problem:
I can't apply this style. I've tried two methods which aren't functional:
By adding the attribute android:style="#style/calculatorNumberStyle" in the "Button" tag of each button.
In this case, the compiler stops the build and raises this error: AAPT: error: attribute android:style not found
By adding the attribute android:theme="#style/calculatorNumberStyle" in the "Button" tag of each button.
In this case, the application is well compiled but the style is not applied.
I've tried to find my way throw this documentation: https://developer.android.com/guide/topics/ui/look-and-feel/themes, but without success.
Does anybody know which is the good method?
SDK platform version: 11.0, AndroidStudio version: 4.1
Thanks by advance and good day at all,
Mickaƫl
Just try to use style without android directive
style="#style/calculatorNumberStyle"
I know that
we have not to use parent attribute. We prefix one style to another
style separating by a period(.)
so in this style, does it have a circular inheritance?
<style name="TextAppearance.A" parent="TextAppearance.A.B">
<item name="android:textAlignment">viewStart</item>
<item name="android:gravity">start</item>
</style>
TextAppearance.A.B inherits from TextAppearance.A because of android dots' syntax.
but TextAppearance.A inherits from TextAppearance.A.B because of android paretn syntax.
Is it really a problem?
Technically As per Android Documentation I dont think this is possible,
Because this will lead to duplication of style, If you refer to same as Diamond Problem it will be one of those, also android prevents you from inheriting from more than one style.
Further Imagine if you have one attribute which is defined in style A also in Style B, it will be a problem at compile time that which attribute to choose from both.
For More Details please refer to android documentation
https://developer.android.com/guide/topics/ui/look-and-feel/themes
I know that
we have not to use parent attribute. We prefix one style to another
style separating by a period(.)
so in this style, does it have a circular inheritance?
<style name="TextAppearance.A" parent="TextAppearance.A.B">
<item name="android:textAlignment">viewStart</item>
<item name="android:gravity">start</item>
</style>
TextAppearance.A.B inherits from TextAppearance.A because of android dots' syntax.
but TextAppearance.A inherits from TextAppearance.A.B because of android paretn syntax.
Is it really a problem?
Technically As per Android Documentation I dont think this is possible,
Because this will lead to duplication of style, If you refer to same as Diamond Problem it will be one of those, also android prevents you from inheriting from more than one style.
Further Imagine if you have one attribute which is defined in style A also in Style B, it will be a problem at compile time that which attribute to choose from both.
For More Details please refer to android documentation
https://developer.android.com/guide/topics/ui/look-and-feel/themes
I know that
we have not to use parent attribute. We prefix one style to another
style separating by a period(.)
so in this style, does it have a circular inheritance?
<style name="TextAppearance.A" parent="TextAppearance.A.B">
<item name="android:textAlignment">viewStart</item>
<item name="android:gravity">start</item>
</style>
TextAppearance.A.B inherits from TextAppearance.A because of android dots' syntax.
but TextAppearance.A inherits from TextAppearance.A.B because of android paretn syntax.
Is it really a problem?
Technically As per Android Documentation I dont think this is possible,
Because this will lead to duplication of style, If you refer to same as Diamond Problem it will be one of those, also android prevents you from inheriting from more than one style.
Further Imagine if you have one attribute which is defined in style A also in Style B, it will be a problem at compile time that which attribute to choose from both.
For More Details please refer to android documentation
https://developer.android.com/guide/topics/ui/look-and-feel/themes