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
Styles
Attributes
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
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.
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
Java:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.AutoMateCyan" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/automate_PrimaryColorCyan</item>
<item name="colorPrimaryVariant">@color/automate_PrimaryVariantCyan</item>
<item name="colorOnPrimary">@color/automate_ColorOnPrimaryCyan</item>
<item name="android:textColorPrimary">@color/automate_textColorPrimaryCyan</item>
<item name="android:windowBackground">@color/automate_windowBackgroundCyan</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/automate_SecondaryColorCyan</item> // background
<item name="colorOnSecondary">@color/automate_ColorOnSecondaryCyan</item>
<item name="android:textColorSecondary">@color/automate_SecondaryColorCyan</item>
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryDark</item>
<item name="android:navigationBarColor">@color/automate_navigationBarColorCyan</item>
<item name="colorAccent">@color/automate_ColorAccentCyan</item>
<!--Custom attributes (in attrs.xml)-->
<item name="automate_PrimaryColor">@color/automate_primary_Cyan</item>
<item name="automate_PrimaryTextColor">@color/automate_primaryText_Cyan</item>
<item name="automate_BorderDarkColor">@color/automate_borderDark_Cyan</item>
<item name="automate_BorderLightColor">@color/automate_borderLight_Cyan</item>
<item name="automate_LabelTextColor">@color/automate_labelText_Cyan</item>
</style>
</resources>
Styles
Java:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AutoMateTextBox" parent="android:Widget.TextView">
<item name="android:textColor">?android:textColorPrimary</item>
<item name="android:paddingStart">5dp</item>
<item name="android:paddingEnd">5dp</item>
<item name="android:typeface">sans</item>
<item name="android:background">@drawable/box_style_rounded_5pt</item>
<item name="android:textSize">20sp</item>
</style>
<style name="AutoMateMyHeader" parent="android:Widget.TextView">
<item name="android:background">@drawable/box_style_header</item>
<item name="android:paddingStart">5dp</item>
<item name="android:paddingEnd">5dp</item>
<item name="android:paddingTop">4dp</item>
<item name="android:paddingBottom">4dp</item>
<item name="android:textAlignment">textStart</item>
<item name="android:typeface">sans</item>
<item name="android:textColor">@color/white</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
</style>
<style name="AutoMateMyEditBox" parent="android:Widget.EditText">
<item name="android:textColor">?android:textColorPrimary</item>
<item name="android:paddingStart">5dp</item>
<item name="android:paddingEnd">5dp</item>
<item name="android:typeface">sans</item>
<item name="android:background">@drawable/box_style_rounded_5pt</item>
<item name="android:textSize">20sp</item>
</style>
<style name="AutoMateMyEditBoxInverted" parent="android:Widget.EditText">
<item name="android:textColor">?android:textColorPrimary</item>
<item name="android:paddingStart">5dp</item>
<item name="android:paddingEnd">5dp</item>
<item name="android:typeface">sans</item>
<item name="android:background">@drawable/box_style_rounded_5pt_inverted</item>
<item name="android:textSize">20sp</item>
</style>
</resources>
Attributes
Java:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="automate_PrimaryColor" format="reference" />
<attr name="automate_PrimaryTextColor" format="reference" />
<attr name="automate_BorderDarkColor" format="reference" />
<attr name="automate_BorderLightColor" format="reference" />
<attr name="automate_LabelTextColor" format="reference" />
</resources>
Java:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false">
The textview using my headerStyle
<TextView
android:id="@+id/headerText"
style="@style/AutoMateMyHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</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
Java:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?android:attr/colorPrimary"/>
</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.