Reputation: 21610
Any searches for any information on how to provide your own custom state for use in a drawable state list selector pulls up very little but almost all of them (here and elsewhere) refer to this google groups post.
I have getters and setters like this: (Not included in the above post, but using their wording to keep it simple)
public void setFried(boolean fried){
if(mFried != fried){
mFried = fried;
refreshDrawableState();
}
}
public void isFried(){
return mFried;
}
I have been trying to get it to work for the last couple hours and nothing seems to be working. It just simply does not change the appearance. I watched what happened as it called onCreateDrawableState(), and I watched what would come out of getDrawableState() after I changed the custom state. The custom state values are in fact appearing in the drawableState array.
Since I can see the state is actually being merged into the array, and since it seems to be completely ignoring any of my custom state in the selector, I think the xml must be wrong.
Here is what the post suggested:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/my.app.package">
<item android:drawable="@drawable/item_baked" state_baked="true"
state_fried="false" />
<item android:drawable="@drawable/item_fried" state_baked="false"
state_fried="true" />
<item android:drawable="@drawable/item_overcooked" state_baked="true"
state_fried="true" />
<item android:drawable="@drawable/item_raw" state_baked="false"
state_fried="false" />
</selector>
Can you really just write state_fried and state_baked or do they need a prefix like app:state_fried? If I try adding a prefix I get a Console error like: No resource identifier found for attribute 'state_fried' in package my.app.package
Has anyone actually got custom states to work? Is the referenced post all you need to get this to work or is there something wrong with it or missing?
I don't know if it makes any difference but I am using an Android Library Project and the selector and the attr.xml is in the Library project.
Thanks
Update Looks like the problem is that Library Projects don't play well with custom attributes. See here, here, here.
Haven't seen much of a workaround unfortunately...
Upvotes: 3
Views: 1319
Reputation: 15
use
xmlns:app="http://schemas.android.com/apk/lib/my.app.package
for attributes declared in the library.
Upvotes: 1
Reputation: 234847
I wrote that original question. IIRC, that was when Android was in beta, and things have changed a bit. Custom attributes do indeed work; I use them. You do indeed need the app:
prefix for custom state attributes. You also need to substitute your app's package in place of my.app.package
in the xmlns declaration.
Upvotes: 2