Skip to content

Instantly share code, notes, and snippets.

@bubbleguuum
Created September 27, 2018 23:02
Show Gist options
  • Save bubbleguuum/a9430813854e2ba60e8607c53a759058 to your computer and use it in GitHub Desktop.
Save bubbleguuum/a9430813854e2ba60e8607c53a759058 to your computer and use it in GitHub Desktop.
package com.bubblesoft.android.utils;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;
public class IconToast {
private static final int ANIMATION_DURATION = 600;
private static final int HIDE_DELAY = 2000;
private View mContainer;
private ImageView mImageView;
private TextView mTextView;
private boolean mShowing;
private Handler mHandler;
private AlphaAnimation mFadeInAnimation;
private AlphaAnimation mFadeOutAnimation;
public IconToast(Activity activity, int layoutResId) {
ViewGroup container = (ViewGroup) activity.findViewById(android.R.id.content);
View v = activity.getLayoutInflater().inflate(layoutResId, container);
init(v);
}
public IconToast(View v) {
init(v);
}
private void init(View v) {
mContainer = v.findViewById(R.id.container);
mContainer.setVisibility(View.GONE);
mTextView = v.findViewById(R.id.text);
mImageView = v.findViewById(R.id.icon);
mFadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
mFadeInAnimation.setDuration(ANIMATION_DURATION);
mFadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
mFadeOutAnimation.setDuration(ANIMATION_DURATION);
mFadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mContainer.setVisibility(View.GONE);
mShowing = false;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mHandler = new Handler();
}
public void show(Drawable drawable, String text) {
mContainer.setVisibility(View.VISIBLE);
mTextView.setVisibility(text == null ? View.GONE : View.VISIBLE);
mTextView.setText(text);
mImageView.setVisibility(drawable == null ? View.GONE : View.VISIBLE);
mImageView.setImageDrawable(drawable);
if(mShowing) {
mHandler.removeCallbacks(mHideRunnable);
} else {
mContainer.startAnimation(mFadeInAnimation);
}
mHandler.postDelayed(mHideRunnable, HIDE_DELAY);
mShowing = true;
}
private final Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
mContainer.startAnimation(mFadeOutAnimation);
}
};
}
@Zukero
Copy link

Zukero commented Jan 10, 2019

Very nice. Thank you.
What is the licence on this? Can I use it in a "GPL v2 or later" project?
Can I change the package name, to avoid namespace fragmentation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment