Android - State Selector Animations - Only animate de-press, NOT press

Reid Conner picture Reid Conner · Nov 7, 2014 · Viewed 20.8k times · Source

I have a very basic selector that I am using as the background for some buttons to achieve down states. Here's the xml:

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

With this selector the fade animation will occur every time the button changes state. In other words, the transition will animate both when going from de-pressed to pressed and also when going back from pressed to de-pressed.

Now, my question is: is it possible to make it so that only one of these state changes animates? When a user presses the button, I want the downstate transition to occur immediately without animations. When the button becomes de-pressed, I want the downstate to fade out while the normal state fades back in. Can this be done?

Answer

PsychedelicSubstance picture PsychedelicSubstance · Apr 5, 2017

Animations on selector : (my drawables were colors )

De-Press (Out)
android:exitFadeDuration="@android:integer/config_shortAnimTime"
Press (In)
android:enterFadeDuration="@android:integer/config_shortAnimTime"

Full example:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
      android:enterFadeDuration="@android:integer/config_shortAnimTime"
      android:exitFadeDuration="@android:integer/config_shortAnimTime">

<item android:state_checked="false" android:drawable="@color/transparent"/>
<item android:state_checked="true"  android:drawable="@drawable/circle_blue"/>
</selector>