Hi i want to set selector to image through code. I have a selector which diff images for diff image view state . There is a way to set selector through xml file but i want to set it on button click .
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/kanal_bogen_rund" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/kanal_bogen_rund_rollover" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/kanal_bogen_rund_rollover" />
<item android:drawable="@drawable/kanal_bogen_rund" />
</selector>
My Code is :-
img.setImageResource(R.layout.my_button);
Or there is any way to change image on focus/hover in android?
Please suggest me usable link or sample code.
This selector depends on view’s state.
For example set this view focused:
and the first item with
state_focused="true"will be used.If you want to switch drawables programmaticaly then use Level List:
http://developer.android.com/guide/topics/resources/drawable-resource.html#LevelList