Issue
I got the following shape drawable:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#222222" />
<stroke android:width="1dip" android:color="?attr/colorPrimary"/>
<corners
android:topLeftRadius="58dp"
android:topRightRadius="58dp"
android:bottomRightRadius="58dp"
android:bottomLeftRadius="58dp">
</corners>
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" />
</shape>
I am setting color attribute from my theme in this line:
<stroke android:width="1dip" android:color="?attr/colorPrimary"/>
Project compiles alright without any issues, but during runtime on a device I got the following problem:
android.view.InflateException: Binary XML file line #119: Error inflating class TextView
...
Caused by: android.content.res.Resources$NotFoundException: File
res/drawable/bordered_green_solid_textview.xml from drawable resource ID #0x7f07006a
When I replace ?attr/colorPrimary and use for example hex color like #222222 it will run without any problem.
What should I do to use ?attrs in my drawables without problems?
P.S.: My min API level 19
My TextView:
<TextView android:id="@+id/imagesCounter"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="35dp"
android:layout_marginBottom="35dp"
android:background="@drawable/bordered_green_solid_textview"
android:gravity="center"
android:text="16"
android:textColor="?attr/colorPrimary"
android:textSize="9sp"
app:layout_constraintBottom_toBottomOf="@+id/imageBorder"
app:layout_constraintStart_toStartOf="@+id/imageBorder"
tools:text="67" />
Solution
After all I found out that for API level like 19 the only possible way is to make separate drawables for different themes of your application.
So first of all you need to declare you new attributes for drawables in values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<attr name="nav_bar_header_drawable" format="reference"/>
...
</resources>
Then in your theme you should set drawable for this attr and now you can reference it anywhere you want.
<style name="Theme.MyDarkMaterialDesign" parent="Theme.MaterialComponents.NoActionBar">
...
<item name="nav_bar_header_drawable">@drawable/side_nav_bar</item>
...
</style>
It works perfectly for API 19 and may be below (didnt test since I only need 19 API and above).
Answered By - Sergei Yendiyarov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.