Issue
I'm trying to create complex drawable with one picture inside changing with main view state change. My current resource looks like that
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<layer-list>
<item>
<selector>
<item android:state_activated="true" android:drawable="@drawable/img_activated_icon" />
<item android:drawable="@drawable/img_default_icon" />
</selector>
...
Next I use it as src for my ImageView. The whole drawable renders fine but the selector ignores setActive() call on the ImageView and always displays "default" icon.
What am I doing wrong?
UPD
So, here goes the desired drawable hierarchy:
layer-list
transition
background1
background2
image
I was unable to activate transition animation in this case so I moved the transition to the upper level:
transition
layer-list
background1
image
layer-list
background2
image (the same image!)
So, how can I implement the first layout and be able both to control the transition and to change source for the image?
Solution
So basically you want to be able to control the drawables manually, and have them react to state changes. To my knowledge, it is bad practice to try to set a fake state (pressed/focused/etc) on a View
, so I would recommend you don't try to do that. I haven't done this before, but I can't see why it couldn't be done. My idea would be to create a single LevelList
of StateList
s for this button/image.
The LevelList
would provide you to have control over what "theme" the button/image has depending on the level you set it to using setLevel()
. Each level would point to a "themed" StateList
that would have different "themed" drawables for all the various states (pressed/focused/etc).
http://developer.android.com/guide/topics/resources/drawable-resource.html#LevelList http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0" android:drawable="@drawable/state_list_icon_default" />
<item android:maxLevel="1" android:drawable="@drawable/state_list_icon_another_style_1" />
<item android:maxLevel="2" android:drawable="@drawable/state_list_icon_another_style_2" />
</level-list>
Your state_list_icon_default
would have your default drawables, like this (it's up to you what states you want to have drawables for):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/img_default_icon_pressed" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/img_default_icon_focused" />
<item android:state_enabled="false" android:state_focused="true" android:drawable="@drawable/img_default_icon_disabled_focused" />
<item android:state_enabled="false" android:drawable="@drawable/img_default_icon_disabled" />
<item android:drawable="@drawable/img_default_icon"/>
</selector>
Here's some tutorials to see more in depth implementations.
http://iserveandroid.blogspot.com/2010/10/progress-bar-implementation-using-level.html http://sermojohn.wordpress.com/2012/02/04/using-a-state-list-drawable-as-a-button-background-image/
Answered By - Steven Byle
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.