Mike Bryant
Mike Bryant

Reputation: 2475

Use attributes in selector - Android

I'm trying to allow the user to set his own color themes. I've managed to accomplish this with

attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="extra_light" format="reference" />
</resources>

styles.xml

<style name="Green" parent="@style/AppTheme">
    <item name="extra_light">@color/extra_light_green</item>
</style>

colors.xml

<resources>
    <color name="extra_light_green">#C5E26D</color>
</resources>

This works well for most of the application however I have a selector which previously had

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/extra_light_green" />
</selector>

to

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="?attr/extra_light" />
</selector>

Now it crashes.

Here's the logcat, any ideas on how to solve this?

 FATAL EXCEPTION: main
 android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
 at android.view.LayoutInflater.createView(LayoutInflater.java:683)
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at com.android.internal.policy.impl.MiuiPhoneLayoutInflater.onCreateView(MiuiPhoneLayoutInflater.java:44)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:730)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:755)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:816)
at android.view.LayoutInflater.inflate(LayoutInflater.java:559)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:419)

EDIT

Here is where I apply the selector

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/row_menu_title"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/selector_item_news"
        android:gravity="center_vertical"
        android:padding="10dp"
        android:text="Medium Text"
        android:textSize="16sp" />

</LinearLayout>

Upvotes: 8

Views: 4987

Answers (2)

Nikolay Nikiforchuk
Nikolay Nikiforchuk

Reputation: 2048

Attrubutes supported from API 23 Item attributes, on later versions ColorStateList can be created programatically with AppCompatResources and used with required view.

selector menu_item_text_color.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?attr/menu_item_text_color_selected" android:state_checked="true" />
    <item android:color="?attr/menu_item_text_color" /> <!-- Default -->
</selector>

in source code

navigationView.setItemTextColor(AppCompatResources.getColorStateList(this, R.color.menu_item_text_color));

Upvotes: 2

Mikalai Parafeniuk
Mikalai Parafeniuk

Reputation: 1356

I have found a workaround for this issue. It seems that android can't apply color when the drawable is expected.

Create a solid shape drawable named item_news_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="?attr/extra_light"/>
</shape>

And then modify your "selector_item_news.xml"

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/item_news_drawable" />
</selector>

I used color for background not for pressed state. But after this code applied, background of textview is changed regarding application theme.

UPD: I have tested my solution on Android L preview. But on Android 4.4.2 it also crashes with Resources$NotFoundException. Details in this answer

Upvotes: 1

Related Questions