Android fade in / fade out selector

I am trying to ensure that the icon in the ActionBar does not change state discretely, but the animation fades. When I add the android:enterFadeDuration and android:exitFadeDuration tag to the selector tag, my drawable is initially invisible - when I click on it, it changes state to state_pressed (correctly with enter time fade), and when I release it, it returns to its normal visible unselected state.

Do I need to skip something obvious, or is this some kind of mistake?

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="150" android:exitFadeDuration="150"> <item android:drawable="@drawable/filters_toggle_icon_selected" android:state_focused="true"/> <item android:drawable="@drawable/filters_toggle_icon_selected" android:state_pressed="true"/> <item android:drawable="@drawable/filters_toggle_icon" android:state_focused="false" android:state_pressed="false"/> </selector> 
+6
source share
2 answers

Use android:enterFadeDuration="@android:integer/config_mediumAnimTime" and android:exitFadeDuration="@android:integer/config_mediumAnimTime" .

+4
source

I had a similar problem , and my code looked like this:

 <selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="@android:integer/config_mediumAnimTime" android:exitFadeDuration="@android:integer/config_mediumAnimTime" > <item android:state_pressed="true" android:drawable="@color/pressed" /> <item android:drawable="@color/default" /> </selector> 

First I found a hint to get rid of enterFadeDuration and use only exitFadeDuration . This resolved the initial invisibility problem, but the view nevertheless disappeared into invisibility during the first intervention.

Then I modified my structure as follows:

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/default" /> <item> <selector android:enterFadeDuration="@android:integer/config_mediumAnimTime" android:exitFadeDuration="@android:integer/config_mediumAnimTime" > <item android:state_pressed="true" android:drawable="@color/pressed" /> </selector> </item> </layer-list> 

Basically, I just popped the default selection out of the selector. This is a workaround, and it also works for multi-state selectors, but has some notable limitations:

  • by default it is always displayed as a lower level. It works for opaque colors, but transparency can cause unwanted results.
  • If the view starts in one of the states checked by the selector, it is still displayed by default , because the selector still starts as invisible.

It may not be applicable to the original problem, but it is something that needs to be considered to overcome this selector behavior.

+4
source

All Articles