use StateListDrawable for seeting selector by code as:
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); //YOUR IMAGE HERE
//AND FOR BUTTON
button.setBackgroundDrawable(states);//FOR BUTTON
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…