I am relatively new to Java and Android development, so I am sorry for disturbing you. I want to create fade-in animation on long click using AnimationUtils.LoadAnimation(), but I am facing error:
Wrong 1st argument type. Found: 'android.view.View.OnLongClickListener', required: 'android.content.Context'
This is my code:
BasicsButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vib.vibrate(VibrationEffect.createOneShot(500,VibrationEffect.DEFAULT_AMPLITUDE));
}else{
vib.vibrate(500);
}
Animation in = AnimationUtils.loadAnimation(this, R.anim.fadein);
blurView.startAnimation(in);
blurView.setVisibility(View.VISIBLE);
return true;
}
I am unaware of what is wrong, this example seems to work, but not for me.
Thank you in advance. :)
The problem is following line
Animation in = AnimationUtils.loadAnimation(this, R.anim.fadein);
Since the above method is call in anonymous class, this
refers to OnLongClickListener
rather than Activity.
Change it to as follows:
Animation in = AnimationUtils.loadAnimation(<ActivityName>.this, R.anim.fadein);
if you use this code inside a fragment, use getContext() instead of this.
Have you tried the below code:
Vibrator vib = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vib.vibrate(VibrationEffect.createOneShot(500,VibrationEffect.DEFAULT_AMPLITUDE));
}else{
vib.vibrate(500);
}
Animation in = AnimationUtils.loadAnimation(getContext(), R.anim.slide_in_left);
blurView.startAnimation(in);
blurView.setVisibility(View.VISIBLE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With