I want to make a like
button
in my app with lottie animation. I have downloaded a json
file ofvthe animation that I want. It works after clicking. But the heart icon is white as default. After I click it, it gets red with the animation. I want it to get white again after I click a second time. I just can't do it. How can I do it?
ProductActivity.java
public class ProductActivity extends AppCompatActivity {
LottieAnimationView imgIconLike;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product);
imgIconLike = findViewById(R.id.img_icon_like);
}
/* I did isAnimated boolean to handle second time click. Also
try pauseAnimation, cancelAnimation and another else. I can't success it anyways. */
private void registerHandler() {
imgIconLike.setOnClickListener(new View.OnClickListener() {
boolean isAnimated=false;
@Override
public void onClick(View v) {
if (!isAnimated){
imgIconLike.playAnimation();
imgIconLike.setSpeed(3f);
isAnimated=true;}
else {
imgIconLike.cancelAnimation;
isAnimated=false;
}
}
});
}
}
activity_product.xml
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/img_icon_like"
android:layout_width="50dp"
android:layout_height="58dp"
app:layout_constraintBottom_toBottomOf="@+id/textFollow_cost"
app:layout_constraintEnd_toStartOf="@+id/textFollow_cost"
app:layout_constraintTop_toTopOf="@+id/textFollow_cost"
app:lottie_autoPlay="false"
app:lottie_fileName="1087-heart.json"
app:lottie_loop="false" />