Android inflates XML selector to be used in StateListDrawable

Is it possible to inflate a selector created in XML that will be used programmatically by StateListDrawable ?

I found that StateListDrawable has an inflate method, but I cannot find any usage examples.

It would be nice to avoid this:

 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); 

Does anyone know if this is possible and provide an example?

Thanks in advance.

+5
source share
1 answer

It is possible of course. You will find more information on all other drawings available in the state list documentation . I hope this answer is useful to someone even after six years.

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/pressed" android:state_pressed="true" /> <item android:drawable="@drawable/focused" android:state_focused="true" /> <item android:drawable="@drawable/normal" /> </selector> 
0
source

Source: https://habr.com/ru/post/1498781/


All Articles