Search code examples
androidanimationcardview

setAnimation not working in LongClickListener


Why is my setAnimation not working in LongClickListener where as it is working fine in outside the LongClick listener?

This is my java code for adding animation

final Animation shake = AnimationUtils.loadAnimation(NavyashActivity.context, R.anim.shake);
final CardView breakfast = (CardView) rootView.findViewById(R.id.breakfastcard);
breakfast.setOnLongClickListener(new View.OnLongClickListener() {
         @Override
        public boolean onLongClick(View v) {
            Log.e("shake long press","coming ");
            // TODO Auto-generated method stub
            breakfast.setAnimation(shake);
            return true;
        }
    });

Also my shake.xml is as:

<?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <rotate
            android:duration="70"
            android:fromDegrees="-5"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="5"
            android:repeatMode="reverse"
            android:interpolator="@android:anim/linear_interpolator"
            android:toDegrees="5" />
        <translate
            android:fromXDelta="-10"
            android:toXDelta="10"
            android:repeatCount="5"
            android:repeatMode="reverse"
            android:interpolator="@android:anim/linear_interpolator"
            android:duration="70" />
    </set>

and my layout for cardview for which i need shaking is:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:longClickable="true"
    android:clickable="true"    
    android:id="@+id/breakfastcard">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       android:background="@drawable/framebox">

       <TextView
            android:layout_width="wrap_content"
            android:id="@+id/breakfastheading"
            android:gravity="center_horizontal"
            android:layout_gravity="center_horizontal"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"/>

        <TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:id="@+id/breakfastdetails"
           android:layout_margin="2dp"/>
   </LinearLayout>
</android.support.v7.widget.CardView>

I just want that as soon as the user Longclicks the cardview element the card should start shaking so as to notify that on being touched for long time its moving continuosly.


Solution

  • Try to change this:

    breakfast.setAnimation(shake);
    

    To this:

    breakfast.startAnimation(shake);
    

    You are setting the animation but not starting it.