button - Android : How to update the selector(StateListDrawable) programmatically -


i want update selector button programmatically.

i can xml file given below

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_enabled="false"          android:drawable="@drawable/btn_off" />    <item android:state_pressed="true"          android:state_enabled="true"           android:drawable="@drawable/btn_off" />    <item android:state_focused="true"          android:state_enabled="true"           android:drawable="@drawable/btn_on" />    <item android:state_enabled="true"           android:drawable="@drawable/btn_on" /> </selector> 

i want same thing programmatically. have tried given below

private statelistdrawable setimagebuttonstate(int index) {     statelistdrawable states = new statelistdrawable();      states.addstate(new int[] {android.r.attr.statenotneeded},r.drawable.btn_off);      states.addstate(new int[] {android.r.attr.state_pressed, android.r.attr.state_enabled},r.drawable.btn_off);     states.addstate(new int[] {android.r.attr.state_focused, android.r.attr.state_enabled},r.drawable.btn_on);     states.addstate(new int[] {android.r.attr.state_enabled},r.drawable.btn_on);      return states; } 

but didnt work.

and how set android:state_enabled="false" or android:state_enabled="true" programatically.

thanks in advance.

you need use negative value of needed state. e.g.:

states.addstate(new int[] {-android.r.attr.state_enabled},r.drawable.btn_disabled); 

notice "-" sign before android.r.attr.state_enabled.


Comments

Popular posts from this blog

Javascript line number mapping -

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -