0

It is simple to specify the view background drawable with normal/pressed states in the .xml, like

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

</selector>  

But how to create a drawable programmably with the focus or pressed state specified? I didn't find the answer in the developer docs.

mianlaoshu
  • 2,342
  • 3
  • 27
  • 48
  • check [this](http://stackoverflow.com/questions/4697528/replace-selector-images-programmatically) answer – hrskrs Feb 05 '15 at 11:27

1 Answers1

0
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
         getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
         getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
         getResources().getDrawable(R.drawable.normal));
imageView.setImageDrawable(states);
user543
  • 3,623
  • 2
  • 16
  • 14
  • Copy & Paste [http://stackoverflow.com/questions/4697528/replace-selector-images-programmatically](http://stackoverflow.com/questions/4697528/replace-selector-images-programmatically) – M D Feb 05 '15 at 11:30