Skip to content

Instantly share code, notes, and snippets.

@michalfaber
Created November 18, 2014 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalfaber/e9659213afb26635ca76 to your computer and use it in GitHub Desktop.
Save michalfaber/e9659213afb26635ca76 to your computer and use it in GitHub Desktop.
Universal FadeIn FadeOut for Android
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void fadeOut(final View view) {
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
AlphaAnimation alpha = new AlphaAnimation(1f, 0f);
alpha.setDuration(mShortAnimationDuration);
alpha.setFillAfter(true);
view.startAnimation(alpha);
} else {
view.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
}
});
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void fadeIn(View view) {
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
view.setVisibility(View.VISIBLE);
AlphaAnimation alpha = new AlphaAnimation(0f, 1f);
alpha.setDuration(mShortAnimationDuration);
alpha.setFillAfter(true);
view.startAnimation(alpha);
} else {
view.setAlpha(0f);
view.setVisibility(View.VISIBLE);
view.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration)
.setListener(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment