1. Are you ready for the Galaxy S20? Here is everything we know so far!

inflator errors using ?attr/colors in styles

Discussion in 'Android Development' started by frappydan, Jun 8, 2021.

  1. frappydan

    frappydan Lurker
    Thread Starter

    I've been working on an app for awhile and finally decided to add support for different themes. I've read a LOT of articles and seen a lot of examples. What I've ended up doing is creating a list of colors for every theme, created definitions to them in my attrs.xml and added them as custom colors in my themes.xml file. Each time I try to use one of the colors in the theme (i.e. ?attr/automate_PrimaryColorCyan) and assign it to either background or textcolor the inflator would crash and close my app. I then added

    implementation 'com.android.support:design:26.1.0' to my gradle build (by suggestion)

    to support my actions. This caused android studio to bark about this implementation and had me migrate the implementation to androidX. After syncing my gradle build I was able to actually assign these values to items but instead of producing the color I wanted I ended up with an extremely dark gray background instead of the color defined in my @colors file. Everything appears normal in the designer. My minimum api is 26 and the target is 30. Pulling my hair out with this problem. Here's the code for my theme, attrs.xml and styles.xml

    Theme
    Code (Java):
    1. <resources xmlns:tools="http://schemas.android.com/tools">
    2.     <!-- Base application theme. -->
    3.     <style name="Theme.AutoMateCyan" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    4.         <!-- Primary brand color. -->
    5.         <item name="colorPrimary">@color/automate_PrimaryColorCyan</item>
    6.         <item name="colorPrimaryVariant">@color/automate_PrimaryVariantCyan</item>
    7.         <item name="colorOnPrimary">@color/automate_ColorOnPrimaryCyan</item>
    8.         <item name="android:textColorPrimary">@color/automate_textColorPrimaryCyan</item>
    9.         <item name="android:windowBackground">@color/automate_windowBackgroundCyan</item>
    10.  
    11.         <!-- Secondary brand color. -->
    12.         <item name="colorSecondary">@color/automate_SecondaryColorCyan</item> // background
    13.         <item name="colorOnSecondary">@color/automate_ColorOnSecondaryCyan</item>
    14.         <item name="android:textColorSecondary">@color/automate_SecondaryColorCyan</item>
    15.  
    16.         <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryDark</item>
    17.         <item name="android:navigationBarColor">@color/automate_navigationBarColorCyan</item>
    18.         <item name="colorAccent">@color/automate_ColorAccentCyan</item>
    19.  
    20.         <!--Custom attributes (in attrs.xml)-->
    21.  
    22.         <item name="automate_PrimaryColor">@color/automate_primary_Cyan</item>
    23.         <item name="automate_PrimaryTextColor">@color/automate_primaryText_Cyan</item>
    24.         <item name="automate_BorderDarkColor">@color/automate_borderDark_Cyan</item>
    25.         <item name="automate_BorderLightColor">@color/automate_borderLight_Cyan</item>
    26.         <item name="automate_LabelTextColor">@color/automate_labelText_Cyan</item>
    27.  
    28.     </style>
    29.  
    30. </resources>
    Styles
    Code (Java):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <resources>
    3.     <style name="AutoMateTextBox" parent="android:Widget.TextView">
    4.         <item name="android:textColor">?android:textColorPrimary</item>
    5.         <item name="android:paddingStart">5dp</item>
    6.         <item name="android:paddingEnd">5dp</item>
    7.         <item name="android:typeface">sans</item>
    8.         <item name="android:background">@drawable/box_style_rounded_5pt</item>
    9.         <item name="android:textSize">20sp</item>
    10.     </style>
    11.     <style name="AutoMateMyHeader" parent="android:Widget.TextView">
    12.         <item name="android:background">@drawable/box_style_header</item>
    13.         <item name="android:paddingStart">5dp</item>
    14.         <item name="android:paddingEnd">5dp</item>
    15.         <item name="android:paddingTop">4dp</item>
    16.         <item name="android:paddingBottom">4dp</item>
    17.         <item name="android:textAlignment">textStart</item>
    18.         <item name="android:typeface">sans</item>
    19.         <item name="android:textColor">@color/white</item>
    20.         <item name="android:textSize">20sp</item>
    21.         <item name="android:textStyle">bold</item>
    22.     </style>
    23.  
    24.     <style name="AutoMateMyEditBox" parent="android:Widget.EditText">
    25.         <item name="android:textColor">?android:textColorPrimary</item>
    26.         <item name="android:paddingStart">5dp</item>
    27.         <item name="android:paddingEnd">5dp</item>
    28.         <item name="android:typeface">sans</item>
    29.         <item name="android:background">@drawable/box_style_rounded_5pt</item>
    30.         <item name="android:textSize">20sp</item>
    31.     </style>
    32.     <style name="AutoMateMyEditBoxInverted" parent="android:Widget.EditText">
    33.         <item name="android:textColor">?android:textColorPrimary</item>
    34.         <item name="android:paddingStart">5dp</item>
    35.         <item name="android:paddingEnd">5dp</item>
    36.         <item name="android:typeface">sans</item>
    37.         <item name="android:background">@drawable/box_style_rounded_5pt_inverted</item>
    38.         <item name="android:textSize">20sp</item>
    39.     </style>
    40. </resources>
    Attributes
    Code (Java):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <resources>
    3.  
    4.     <attr name="automate_PrimaryColor" format="reference" />
    5.     <attr name="automate_PrimaryTextColor" format="reference" />
    6.     <attr name="automate_BorderDarkColor" format="reference" />
    7.     <attr name="automate_BorderLightColor" format="reference" />
    8.     <attr name="automate_LabelTextColor" format="reference" />
    9.  
    10.  
    11. </resources>
    Code (Java):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     xmlns:app="http://schemas.android.com/apk/res-auto"
    4.     android:layout_width="match_parent"
    5.     android:layout_height="wrap_content"
    6.     android:clickable="false">
    7.  
    8. The textview using my headerStyle
    9.     <TextView
    10.         android:id="@+id/headerText"
    11.         style="@style/AutoMateMyHeader"
    12.         android:layout_width="0dp"
    13.         android:layout_height="wrap_content"
    14.         app:layout_constraintBottom_toBottomOf="parent"
    15.         app:layout_constraintEnd_toEndOf="parent"
    16.         app:layout_constraintStart_toStartOf="parent"
    17.         app:layout_constraintTop_toTopOf="parent" />
    18. </androidx.constraintlayout.widget.ConstraintLayout>
    The shape file referenced in the AutoMateMyHeader definition
    This should make the background color a nice dark cyan and it's actually very dark gray
    Code (Java):
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
    3.     <solid android:color="?android:attr/colorPrimary"/>
    4. </shape>
    And finally here's what the view looks like when I run the app
    The headers should have the same background color as the toolbar.
    ugly_screen.jpg
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - inflator errors using
  1. willsp28
    Replies:
    1
    Views:
    112
  2. Danilo Pazzelli
    Replies:
    1
    Views:
    239
  3. unet
    Replies:
    0
    Views:
    260
  4. codezer0
    Replies:
    5
    Views:
    327
  5. Yesi Trotamundos
    Replies:
    6
    Views:
    382
  6. Jeremy Hutchings
    Replies:
    5
    Views:
    321
  7. mysorian
    Replies:
    1
    Views:
    329
  8. KLB3
    Replies:
    2
    Views:
    316
  9. Dinesh Pitale
    Replies:
    0
    Views:
    225
  10. kumaranil13k
    Replies:
    10
    Views:
    716

Share This Page

Loading...