Question

I have this listview:

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/imageView9"
    android:divider="#fff"
    android:cacheColorHint="#00000000"
    android:dividerHeight="0.9dp"
    android:listSelector="#80ED84"
    android:choiceMode="singleChoice"
    android:layout_marginBottom="50dp"
    android:background="#fff"
    >

</ListView>

It has a list selector that shows green color when an item inside the list is selected, what i want to create is to fade in the color when the item is selected, instead of it just appearing, how could i animate only the list selector?

I have tried this:

listView = (ListView) findViewById(R.id.list);
            listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> av, final View view, final int i, long i2) {

                  Animation anim = AnimationUtils.loadAnimation(SendActivity.this, R.anim.fade_in);
                  view.startAnimation(anim);

            }
            });  

But that animates the entire listview, and not just the list selector, how could i modify the code?

Was it helpful?

Solution

You must create an XML file for your list selector that you place in /drawable-nodpi and adjust the values of android:enterFadeDuration and android:exitFadeDuration to your desired fade animation duration.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="@android:integer/config_mediumAnimTime" android:exitFadeDuration="@android:integer/config_mediumAnimTime">

    <item android:drawable="@color/a_lighter_green_color" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@color/your_green_color" android:state_pressed="true"/>
    <item android:drawable="@android:color/transparent"/>

</selector>

Please note that with this technique, which is the proper way to do it, the animation will only be visible on API level 11 and above (Android 3+), but today it means more than 80% of devices. Older devices will see the correct color but no fade animation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top