Issue
I was wondering if there was any way in Android to set an ImageView
's image dynamically by using its tag
.
Ideally it would be like this:
// image_view_style.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:tag="1"
android:drawable="@drawable/image_for_tag_1"/>
<item android:tag="2"
android:drawable="@drawable/image_for_tag_2"/>
<item android:tag="3"
android:drawable="@drawable/image_for_tag_3"/>
</selector>
And simply calling the ImageView
's setTag()
method with the value I want.
I tried this but it doesn't work. Is there any clean way to do the image selection in the resource file (for something else than state_selected
, state_pressed
, etc) ?
Solution
I think you can use LevelListDrawable
to achieve what you want:
https://developer.android.com/reference/android/graphics/drawable/LevelListDrawable.html
From the documentation:
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0" android:drawable="@drawable/ic_wifi_signal_1" />
<item android:maxLevel="1" android:drawable="@drawable/ic_wifi_signal_2" />
<item android:maxLevel="2" android:drawable="@drawable/ic_wifi_signal_3" />
<item android:maxLevel="3" android:drawable="@drawable/ic_wifi_signal_4" />
</level-list>
then use setLevel(...)
in code to set a level.
Answered By - Goddchen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.