Setting drawable with colors to a TextView for supporting multiple themes

[postlink] https://the-best-way-of-life-is-islam.blogspot.com/2015/07/setting-drawable-with-colors-to.html [/postlink]

I want to have a list of CardViews like this, for my two themes:

http://ift.tt/1IumRv6

I want to have different background colors for textviews for each theme. So, in my row xml, I have the following for the TextView:

<TextView
      android:id="@+id/txtTipListId"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="17"
      android:background="@drawable/idcardview"
      android:gravity="center|center_vertical"
      android:textColor="@color/colorShadowInverse"
      android:textSize="@dimen/abc_text_size_medium_material"
      android:textStyle="bold" />`

and the drawable "idcardview.xml" is:

<shape xmlns:android="http://ift.tt/nIICcg">
    <solid android:color="?attr/themedCardBackgroundColor"/>
    <corners android:bottomRightRadius="10dp"
        android:topRightRadius="10dp"/>
</shape>    

and in the attrs.xml:

<resources>
    <attr name="themedCardBackgroundColor" format="reference" />
</resources>    

and in the styles:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
    <!-- Customize your theme here. -->
</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="themedCardBackgroundColor">@color/colorShadowInverse</item>

</style>

<style name="AppTheme.Light" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="themedCardBackgroundColor">@color/colorShadow</item>
</style>
</resources>

and in the colors.xml:

<resources>
<color name="colorPrimary">#FF9800</color>
<color name="colorPrimaryDark">#F57C00</color>
<color name="colorPrimaryLight">#FFE0B2</color>
<color name="colorAccent">#FF5252</color>

<color name="colorShadow">#E0E0E0</color>
<color name="colorShadowInverse">#181818</color>

</resources>

When I run the app and go to the list activity, it gets the following error:

android.view.InflateException: Binary XML file line #32: Error inflating class TextView

Caused by: android.content.res.Resources$NotFoundException: File res/drawable/idcardview.xml from drawable resource ID #0x7f02003b

at android.content.res.Resources.loadDrawable(Resources.java:1923)

at android.content.res.TypedArray.getDrawable(TypedArray.java:601)

at android.view.View.<init>(View.java:2785)

but when I change the "idcardview.xml" solid color to just one color (for example, #181818), the app just runs fine. But I want it to change background color for light and dark theme. Using Android Studio, min Sdk 11, target SDK 22.

اذا أعجبك الموضوع ساهم في نشره :

Enregistrer un commentaire

 
حقوق النشر: Published by | Template Created by | القناة على اليوتيب
copyright © 2014. The Best Way Of Life Is Islam -يمكن نقل كل ما هو موجود في موقع الرحمة تيوب
القرآن الكريم و الأحاديث النبوية أناشيد و دروس اسلامية منوعة موقع الفيديوهات الاسلامية
تطوير و تعريب مدونة ثعلوب