Java tutorial
/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.topbottomsnackbar; import android.annotation.TargetApi; import android.content.Context; import android.content.res.ColorStateList; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.VectorDrawable; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.support.annotation.ColorInt; import android.support.annotation.DrawableRes; import android.support.annotation.IntDef; import android.support.annotation.IntRange; import android.support.annotation.NonNull; import android.support.annotation.StringRes; import android.support.design.R; import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.SwipeDismissBehavior; import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewPropertyAnimatorListenerAdapter; import android.support.v4.view.WindowInsetsCompat; import android.text.TextUtils; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import android.view.accessibility.AccessibilityManager; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.TextView; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import static com.github.topbottomsnackbar.AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR; public final class TBSnackbar { /** * Callback class for {@link TBSnackbar} instances. * * @see TBSnackbar#setCallback(Callback) */ public static abstract class Callback { /** * Indicates that the TBSnackbar was dismissed via a swipe. */ public static final int DISMISS_EVENT_SWIPE = 0; /** * Indicates that the TBSnackbar was dismissed via an action click. */ public static final int DISMISS_EVENT_ACTION = 1; /** * Indicates that the TBSnackbar was dismissed via a timeout. */ public static final int DISMISS_EVENT_TIMEOUT = 2; /** * Indicates that the TBSnackbar was dismissed via a call to {@link #dismiss()}. */ public static final int DISMISS_EVENT_MANUAL = 3; /** * Indicates that the TBSnackbar was dismissed from a new TBSnackbar being shown. */ public static final int DISMISS_EVENT_CONSECUTIVE = 4; @IntDef({ DISMISS_EVENT_SWIPE, DISMISS_EVENT_ACTION, DISMISS_EVENT_TIMEOUT, DISMISS_EVENT_MANUAL, DISMISS_EVENT_CONSECUTIVE }) @Retention(RetentionPolicy.SOURCE) public @interface DismissEvent { } /** * Called when the given {@link TBSnackbar} has been dismissed, either through a time-out, * having been manually dismissed, or an action being clicked. * * @param TBSnackbar The TBSnackbar which has been dismissed. * @param event The event which caused the dismissal. One of either: * {@link #DISMISS_EVENT_SWIPE}, {@link #DISMISS_EVENT_ACTION}, * {@link #DISMISS_EVENT_TIMEOUT}, {@link #DISMISS_EVENT_MANUAL} or * {@link #DISMISS_EVENT_CONSECUTIVE}. * @see TBSnackbar#dismiss() */ public void onDismissed(TBSnackbar TBSnackbar, @DismissEvent int event) { // empty } /** * Called when the given {@link TBSnackbar} is visible. * * @param TBSnackbar The TBSnackbar which is now visible. * @see TBSnackbar#show() */ public void onShown(TBSnackbar TBSnackbar) { // empty } } @IntDef({ LENGTH_INDEFINITE, LENGTH_SHORT, LENGTH_LONG }) @IntRange(from = 1) @Retention(RetentionPolicy.SOURCE) public @interface Duration { } @IntDef({ STYLE_SHOW_BOTTOM, STYLE_SHOW_TOP, STYLE_SHOW_TOP_FITSYSTEMWINDOW }) @IntRange(from = 1) @Retention(RetentionPolicy.SOURCE) public @interface Style { } /** * Show the TBSnackbar indefinitely. This means that the TBSnackbar will be displayed from the time * that is {@link #show() shown} until either it is dismissed, or another TBSnackbar is shown. * * @see #setDuration */ public static final int LENGTH_INDEFINITE = -2; /** * Show the TBSnackbar for a short period of time. * * @see #setDuration */ public static final int LENGTH_SHORT = -1; /** * Show the TBSnackbar for a long period of time. * * @see #setDuration */ public static final int LENGTH_LONG = 0; public static final int STYLE_SHOW_TOP = 1; public static final int STYLE_SHOW_BOTTOM = 2; public static final int STYLE_SHOW_TOP_FITSYSTEMWINDOW = 3; static final int ANIMATION_DURATION = 250; static final int ANIMATION_FADE_DURATION = 180; static final Handler sHandler; static final int MSG_SHOW = 0; static final int MSG_DISMISS = 1; static { sHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() { @Override public boolean handleMessage(Message message) { switch (message.what) { case MSG_SHOW: ((TBSnackbar) message.obj).showView(); return true; case MSG_DISMISS: ((TBSnackbar) message.obj).hideView(message.arg1); return true; } return false; } }); } private final ViewGroup mTargetParent; private final Context mContext; final SnackbarLayout mView; private int mDuration; private Callback mCallback; private int mStyle = STYLE_SHOW_BOTTOM; private final AccessibilityManager mAccessibilityManager; private TBSnackbar(ViewGroup parent) { mTargetParent = parent; mContext = parent.getContext(); LayoutInflater inflater = LayoutInflater.from(mContext); mView = (SnackbarLayout) inflater.inflate(R.layout.design_layout_snackbar, mTargetParent, false); mAccessibilityManager = (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE); } private static int getStatusBarHeight(Context context) { int result = 0; int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resId > 0) { result = context.getResources().getDimensionPixelOffset(resId); } return result; } private void changeLayoutParams(int gravity) { if (mTargetParent instanceof CoordinatorLayout) { CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mView.getLayoutParams(); layoutParams.gravity = gravity; mView.setLayoutParams(layoutParams); } else if (mTargetParent instanceof FrameLayout) { FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mView.getLayoutParams(); layoutParams.gravity = gravity; mView.setLayoutParams(layoutParams); } } @Deprecated private TBSnackbar addIcon(int resource_id, int size) { final TextView tv = mView.getMessageView(); tv.setCompoundDrawablesWithIntrinsicBounds(new BitmapDrawable(Bitmap.createScaledBitmap( ((BitmapDrawable) (mContext.getResources().getDrawable(resource_id))).getBitmap(), size, size, true)), null, null, null); return this; } public TBSnackbar setIconPadding(int padding) { final TextView tv = mView.getMessageView(); tv.setCompoundDrawablePadding(padding); return this; } public TBSnackbar setIconLeft(@DrawableRes int drawableRes, float sizeDp) { final TextView tv = mView.getMessageView(); Drawable drawable = ContextCompat.getDrawable(mContext, drawableRes); if (drawable != null) { drawable = fitDrawable(drawable, (int) convertDpToPixel(sizeDp, mContext)); } else { throw new IllegalArgumentException("resource_id is not a valid drawable!"); } final Drawable[] compoundDrawables = tv.getCompoundDrawables(); tv.setCompoundDrawables(drawable, compoundDrawables[1], compoundDrawables[2], compoundDrawables[3]); return this; } public TBSnackbar setIconRight(@DrawableRes int drawableRes, float sizeDp) { final TextView tv = mView.getMessageView(); Drawable drawable = ContextCompat.getDrawable(mContext, drawableRes); if (drawable != null) { drawable = fitDrawable(drawable, (int) convertDpToPixel(sizeDp, mContext)); } else { throw new IllegalArgumentException("resource_id is not a valid drawable!"); } final Drawable[] compoundDrawables = tv.getCompoundDrawables(); tv.setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], drawable, compoundDrawables[3]); return this; } /** * Overrides the max width of this snackbar's layout. This is typically not necessary; the snackbar * width will be according to Google's Material guidelines. Specifically, the max width will be * * To allow the snackbar to have a width equal to the parent view, set a value * * @param maxWidth the max width in pixels * @return this TSnackbar */ public TBSnackbar setMaxWidth(int maxWidth) { mView.mMaxWidth = maxWidth; return this; } private Drawable fitDrawable(Drawable drawable, int sizePx) { if (drawable.getIntrinsicWidth() != sizePx || drawable.getIntrinsicHeight() != sizePx) { if (drawable instanceof BitmapDrawable) { drawable = new BitmapDrawable(mContext.getResources(), Bitmap.createScaledBitmap(getBitmap(drawable), sizePx, sizePx, true)); } } drawable.setBounds(0, 0, sizePx, sizePx); return drawable; } private static float convertDpToPixel(float dp, Context context) { Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); float px = dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT); return px; } @TargetApi(Build.VERSION_CODES.LOLLIPOP) private static Bitmap getBitmap(VectorDrawable vectorDrawable) { Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return bitmap; } private static Bitmap getBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof VectorDrawable) { return getBitmap((VectorDrawable) drawable); } else { throw new IllegalArgumentException("unsupported drawable type"); } } /** * Make a TBSnackbar to display a message * * * @param view The view to find a parent from. * @param text The text to show. Can be formatted text. * @param duration How long to display the message. Either {@link #LENGTH_SHORT} or {@link * #LENGTH_LONG} */ @NonNull public static TBSnackbar make(@NonNull View view, @NonNull CharSequence text, @Duration int duration, @Style int style) { TBSnackbar tbSnackbar = new TBSnackbar(findSuitableParent(view)); tbSnackbar.mStyle = style; tbSnackbar.setText(text); tbSnackbar.setDuration(duration); return tbSnackbar; } /** * Make a TBSnackbar to display a message. *Having a {@link CoordinatorLayout} in your view hierarchy allows TBSnackbar to enable * certain features, such as swipe-to-dismiss and automatically moving of widgets like * * * @param view The view to find a parent from. * @param resId The resource id of the string resource to use. Can be formatted text. * @param duration How long to display the message. Either {@link #LENGTH_SHORT} or {@link * #LENGTH_LONG} */ @NonNull public static TBSnackbar make(@NonNull View view, @StringRes int resId, @Duration int duration, @Style int style) { return make(view, view.getResources().getText(resId), duration, style); } private static ViewGroup findSuitableParent(View view) { ViewGroup fallback = null; do { if (view instanceof CoordinatorLayout) { // We've found a CoordinatorLayout, use it return (ViewGroup) view; } else if (view instanceof FrameLayout) { if (view.getId() == android.R.id.content) { // If we've hit the decor content view, then we didn't find a CoL in the // hierarchy, so use it. return (ViewGroup) view; } else { // It's not the content view but we'll use it as our fallback fallback = (ViewGroup) view; } } if (view != null) { // Else, we will loop and crawl up the view hierarchy and try to find a parent final ViewParent parent = view.getParent(); view = parent instanceof View ? (View) parent : null; } } while (view != null); // If we reach here then we didn't find a CoL or a suitable content view so we'll fallback return fallback; } /** * Set the action to be displayed in this {@link TBSnackbar}. * * @param resId String resource to display * @param listener callback to be invoked when the action is clicked */ @NonNull public TBSnackbar setAction(@StringRes int resId, View.OnClickListener listener) { return setAction(mContext.getText(resId), listener); } /** * Set the action to be displayed in this {@link TBSnackbar}. * * @param text Text to display * @param listener callback to be invoked when the action is clicked */ @NonNull public TBSnackbar setAction(CharSequence text, final View.OnClickListener listener) { final TextView tv = mView.getActionView(); if (TextUtils.isEmpty(text) || listener == null) { tv.setVisibility(View.GONE); tv.setOnClickListener(null); } else { tv.setVisibility(View.VISIBLE); tv.setText(text); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { listener.onClick(view); // Now dismiss the TBSnackbar dispatchDismiss(Callback.DISMISS_EVENT_ACTION); } }); } return this; } /** * Sets the text color of the action specified in * {@link #setAction(CharSequence, View.OnClickListener)}. */ @NonNull public TBSnackbar setActionTextColor(ColorStateList colors) { final TextView tv = mView.getActionView(); tv.setTextColor(colors); return this; } /** * Sets the text color of the action specified in * {@link #setAction(CharSequence, View.OnClickListener)}. */ @NonNull public TBSnackbar setActionTextColor(@ColorInt int color) { final TextView tv = mView.getActionView(); tv.setTextColor(color); return this; } /** * Update the text in this {@link TBSnackbar}. * * @param message The new text for the Toast. */ @NonNull public TBSnackbar setText(@NonNull CharSequence message) { final TextView tv = mView.getMessageView(); tv.setText(message); return this; } /** * Update the text in this {@link TBSnackbar}. * * @param resId The new text for the Toast. */ @NonNull public TBSnackbar setText(@StringRes int resId) { return setText(mContext.getText(resId)); } /** * Set how long to show the view for. * * @param duration either be one of the predefined lengths: * {@link #LENGTH_SHORT}, {@link #LENGTH_LONG}, or a custom duration * in milliseconds. */ @NonNull public TBSnackbar setDuration(@Duration int duration) { mDuration = duration; return this; } /** * Return the duration. * * @see #setDuration */ @Duration public int getDuration() { return mDuration; } /** * Returns the {@link TBSnackbar}'s view. */ @NonNull public View getView() { return mView; } /** * Show the {@link TBSnackbar}. */ public void show() { if (mStyle == STYLE_SHOW_BOTTOM) { changeLayoutParams(Gravity.BOTTOM); } else { changeLayoutParams(Gravity.TOP); if (mStyle == STYLE_SHOW_TOP_FITSYSTEMWINDOW) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) mView.setPadding(0, getStatusBarHeight(mContext), 0, 0); } } TBSnackbarManager.getInstance().show(mDuration, mManagerCallback); } /** * Dismiss the {@link TBSnackbar}. */ public void dismiss() { dispatchDismiss(Callback.DISMISS_EVENT_MANUAL); } void dispatchDismiss(@Callback.DismissEvent int event) { TBSnackbarManager.getInstance().dismiss(mManagerCallback, event); } /** * Set a callback to be called when this the visibility of this {@link TBSnackbar} changes. */ @NonNull public TBSnackbar setCallback(Callback callback) { mCallback = callback; return this; } /** * Return whether this {@link TBSnackbar} is currently being shown. */ public boolean isShown() { return TBSnackbarManager.getInstance().isCurrent(mManagerCallback); } /** * Returns whether this {@link TBSnackbar} is currently being shown, or is queued to be * shown next. * @return */ public boolean isShownOrQueued() { return TBSnackbarManager.getInstance().isCurrentOrNext(mManagerCallback); } final TBSnackbarManager.Callback mManagerCallback = new TBSnackbarManager.Callback() { @Override public void show() { sHandler.sendMessage(sHandler.obtainMessage(MSG_SHOW, TBSnackbar.this)); } @Override public void dismiss(int event) { sHandler.sendMessage(sHandler.obtainMessage(MSG_DISMISS, event, 0, TBSnackbar.this)); } }; final void showView() { if (mView.getParent() == null) { final ViewGroup.LayoutParams lp = mView.getLayoutParams(); if (lp instanceof CoordinatorLayout.LayoutParams && mStyle == STYLE_SHOW_BOTTOM) { // If our LayoutParams are from a CoordinatorLayout, we'll setup our Behavior final CoordinatorLayout.LayoutParams clp = (CoordinatorLayout.LayoutParams) lp; final Behavior behavior = new Behavior(); behavior.setStartAlphaSwipeDistance(0.1f); behavior.setEndAlphaSwipeDistance(0.6f); behavior.setSwipeDirection(SwipeDismissBehavior.SWIPE_DIRECTION_START_TO_END); behavior.setListener(new SwipeDismissBehavior.OnDismissListener() { @Override public void onDismiss(View view) { view.setVisibility(View.GONE); dispatchDismiss(Callback.DISMISS_EVENT_SWIPE); } @Override public void onDragStateChanged(int state) { switch (state) { case SwipeDismissBehavior.STATE_DRAGGING: case SwipeDismissBehavior.STATE_SETTLING: // If the view is being dragged or settling, cancel the timeout TBSnackbarManager.getInstance().cancelTimeout(mManagerCallback); break; case SwipeDismissBehavior.STATE_IDLE: // If the view has been released and is idle, restore the timeout TBSnackbarManager.getInstance().restoreTimeout(mManagerCallback); break; } } }); clp.setBehavior(behavior); // Also set the inset edge so that views can dodge the snackbar correctly clp.insetEdge = Gravity.BOTTOM; } mTargetParent.addView(mView); } mView.setOnAttachStateChangeListener(new SnackbarLayout.OnAttachStateChangeListener() { @Override public void onViewAttachedToWindow(View v) { } @Override public void onViewDetachedFromWindow(View v) { if (isShownOrQueued()) { // If we haven't already been dismissed then this event is coming from a // non-user initiated action. Hence we need to make sure that we callback // and keep our state up to date. We need to post the call since removeView() // will call through to onDetachedFromWindow and thus overflow. sHandler.post(new Runnable() { @Override public void run() { onViewHidden(Callback.DISMISS_EVENT_MANUAL); } }); } } }); if (ViewCompat.isLaidOut(mView)) { if (shouldAnimate()) { // If animations are enabled, animate it in animateViewIn(); } else { // Else if anims are disabled just call back now onViewShown(); } } else { // Otherwise, add one of our layout change listeners and show it in when laid out mView.setOnLayoutChangeListener(new SnackbarLayout.OnLayoutChangeListener() { @Override public void onLayoutChange(View view, int left, int top, int right, int bottom) { mView.setOnLayoutChangeListener(null); if (shouldAnimate()) { // If animations are enabled, animate it in animateViewIn(); } else { // Else if anims are disabled just call back now onViewShown(); } } }); } } void animateViewIn() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { if (mStyle == STYLE_SHOW_BOTTOM) { in(mView.getHeight()); } else { in(-mView.getHeight()); } } else { if (mStyle == STYLE_SHOW_BOTTOM) { in2(com.github.topbottomsnackbar.R.anim.design_snackbar_in); } else { in2(com.github.topbottomsnackbar.R.anim.top_in); } } } private void in2(int animId) { Animation anim = AnimationUtils.loadAnimation(mView.getContext(), animId); anim.setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR); anim.setDuration(ANIMATION_DURATION); anim.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationEnd(Animation animation) { onViewShown(); } @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } }); mView.startAnimation(anim); } private void in(int height) { ViewCompat.setTranslationY(mView, height); ViewCompat.animate(mView).translationY(0f).setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR) .setDuration(ANIMATION_DURATION).setListener(new ViewPropertyAnimatorListenerAdapter() { @Override public void onAnimationStart(View view) { mView.animateChildrenIn(ANIMATION_DURATION - ANIMATION_FADE_DURATION, ANIMATION_FADE_DURATION); } @Override public void onAnimationEnd(View view) { onViewShown(); } }).start(); } private void animateViewOut(final int event) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { if (mStyle == STYLE_SHOW_BOTTOM) { out(event, mView.getHeight()); } else { out(event, -mView.getHeight()); } } else { if (mStyle == STYLE_SHOW_BOTTOM) { out2(event, com.github.topbottomsnackbar.R.anim.design_snackbar_out); } else { out2(event, com.github.topbottomsnackbar.R.anim.top_out); } } } private void out2(final int event, int animId) { Animation anim = AnimationUtils.loadAnimation(mView.getContext(), animId); anim.setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR); anim.setDuration(ANIMATION_DURATION); anim.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationEnd(Animation animation) { onViewHidden(event); } @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } }); mView.startAnimation(anim); } private void out(final int event, int height) { ViewCompat.animate(mView).translationY(height).setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR) .setDuration(ANIMATION_DURATION).setListener(new ViewPropertyAnimatorListenerAdapter() { @Override public void onAnimationStart(View view) { mView.animateChildrenOut(0, ANIMATION_FADE_DURATION); } @Override public void onAnimationEnd(View view) { onViewHidden(event); } }).start(); } final void hideView(@Callback.DismissEvent final int event) { if (shouldAnimate() && mView.getVisibility() == View.VISIBLE) { animateViewOut(event); } else { // If anims are disabled or the view isn't visible, just call back now onViewHidden(event); } } void onViewShown() { TBSnackbarManager.getInstance().onShown(mManagerCallback); if (mCallback != null) { mCallback.onShown(this); } } void onViewHidden(int event) { // First tell the TBSnackbarManager that it has been dismissed TBSnackbarManager.getInstance().onDismissed(mManagerCallback); // Now call the dismiss listener (if available) if (mCallback != null) { mCallback.onDismissed(this, event); } if (Build.VERSION.SDK_INT < 11) { // We need to hide the TBSnackbar on pre-v11 since it uses an old style Animation. // ViewGroup has special handling in removeView() when getAnimation() != null in // that it waits. This then means that the calculated insets are wrong and the // any dodging views do not return. We workaround it by setting the view to gone while // ViewGroup actually gets around to removing it. mView.setVisibility(View.GONE); } // Lastly, hide and remove the view from the parent (if attached) final ViewParent parent = mView.getParent(); if (parent instanceof ViewGroup) { ((ViewGroup) parent).removeView(mView); } } /** * Returns true if we should animate the TBSnackbar view in/out. */ boolean shouldAnimate() { return !mAccessibilityManager.isEnabled(); } public static class SnackbarLayout extends LinearLayout { private TextView mMessageView; private Button mActionView; private int mMaxWidth; private int mMaxInlineActionWidth; interface OnLayoutChangeListener { void onLayoutChange(View view, int left, int top, int right, int bottom); } interface OnAttachStateChangeListener { void onViewAttachedToWindow(View v); void onViewDetachedFromWindow(View v); } private OnLayoutChangeListener mOnLayoutChangeListener; private OnAttachStateChangeListener mOnAttachStateChangeListener; public SnackbarLayout(Context context) { this(context, null); } public SnackbarLayout(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SnackbarLayout); mMaxWidth = a.getDimensionPixelSize(R.styleable.SnackbarLayout_android_maxWidth, -1); mMaxInlineActionWidth = a.getDimensionPixelSize(R.styleable.SnackbarLayout_maxActionInlineWidth, -1); if (a.hasValue(R.styleable.SnackbarLayout_elevation)) { ViewCompat.setElevation(this, a.getDimensionPixelSize(R.styleable.SnackbarLayout_elevation, 0)); } a.recycle(); setClickable(true); // Now inflate our content. We need to do this manually rather than using an <include> // in the layout since older versions of the Android do not inflate includes with // the correct Context. LayoutInflater.from(context).inflate(R.layout.design_layout_snackbar_include, this); ViewCompat.setAccessibilityLiveRegion(this, ViewCompat.ACCESSIBILITY_LIVE_REGION_POLITE); ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES); // Make sure that we fit system windows and have a listener to apply any insets ViewCompat.setFitsSystemWindows(this, true); ViewCompat.setOnApplyWindowInsetsListener(this, new android.support.v4.view.OnApplyWindowInsetsListener() { @Override public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { // Copy over the bottom inset as padding so that we're displayed above the // navigation bar v.setPadding(v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), insets.getSystemWindowInsetBottom()); return insets; } }); } @Override protected void onFinishInflate() { super.onFinishInflate(); mMessageView = (TextView) findViewById(R.id.snackbar_text); mActionView = (Button) findViewById(R.id.snackbar_action); } TextView getMessageView() { return mMessageView; } Button getActionView() { return mActionView; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mMaxWidth > 0 && getMeasuredWidth() > mMaxWidth) { widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } final int multiLineVPadding = getResources() .getDimensionPixelSize(R.dimen.design_snackbar_padding_vertical_2lines); final int singleLineVPadding = getResources() .getDimensionPixelSize(R.dimen.design_snackbar_padding_vertical); final boolean isMultiLine = mMessageView.getLayout().getLineCount() > 1; boolean remeasure = false; if (isMultiLine && mMaxInlineActionWidth > 0 && mActionView.getMeasuredWidth() > mMaxInlineActionWidth) { if (updateViewsWithinLayout(VERTICAL, multiLineVPadding, multiLineVPadding - singleLineVPadding)) { remeasure = true; } } else { final int messagePadding = isMultiLine ? multiLineVPadding : singleLineVPadding; if (updateViewsWithinLayout(HORIZONTAL, messagePadding, messagePadding)) { remeasure = true; } } if (remeasure) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } void animateChildrenIn(int delay, int duration) { ViewCompat.setAlpha(mMessageView, 0f); ViewCompat.animate(mMessageView).alpha(1f).setDuration(duration).setStartDelay(delay).start(); if (mActionView.getVisibility() == VISIBLE) { ViewCompat.setAlpha(mActionView, 0f); ViewCompat.animate(mActionView).alpha(1f).setDuration(duration).setStartDelay(delay).start(); } } void animateChildrenOut(int delay, int duration) { ViewCompat.setAlpha(mMessageView, 1f); ViewCompat.animate(mMessageView).alpha(0f).setDuration(duration).setStartDelay(delay).start(); if (mActionView.getVisibility() == VISIBLE) { ViewCompat.setAlpha(mActionView, 1f); ViewCompat.animate(mActionView).alpha(0f).setDuration(duration).setStartDelay(delay).start(); } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (mOnLayoutChangeListener != null) { mOnLayoutChangeListener.onLayoutChange(this, l, t, r, b); } } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (mOnAttachStateChangeListener != null) { mOnAttachStateChangeListener.onViewAttachedToWindow(this); } ViewCompat.requestApplyInsets(this); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); if (mOnAttachStateChangeListener != null) { mOnAttachStateChangeListener.onViewDetachedFromWindow(this); } } void setOnLayoutChangeListener(OnLayoutChangeListener onLayoutChangeListener) { mOnLayoutChangeListener = onLayoutChangeListener; } void setOnAttachStateChangeListener(OnAttachStateChangeListener listener) { mOnAttachStateChangeListener = listener; } private boolean updateViewsWithinLayout(final int orientation, final int messagePadTop, final int messagePadBottom) { boolean changed = false; if (orientation != getOrientation()) { setOrientation(orientation); changed = true; } if (mMessageView.getPaddingTop() != messagePadTop || mMessageView.getPaddingBottom() != messagePadBottom) { updateTopBottomPadding(mMessageView, messagePadTop, messagePadBottom); changed = true; } return changed; } private static void updateTopBottomPadding(View view, int topPadding, int bottomPadding) { if (ViewCompat.isPaddingRelative(view)) { ViewCompat.setPaddingRelative(view, ViewCompat.getPaddingStart(view), topPadding, ViewCompat.getPaddingEnd(view), bottomPadding); } else { view.setPadding(view.getPaddingLeft(), topPadding, view.getPaddingRight(), bottomPadding); } } } final class Behavior extends SwipeDismissBehavior<SnackbarLayout> { @Override public boolean canSwipeDismissView(View child) { return child instanceof SnackbarLayout; } @Override public boolean onInterceptTouchEvent(CoordinatorLayout parent, SnackbarLayout child, MotionEvent event) { // We want to make sure that we disable any TBSnackbar timeouts if the user is // currently touching the TBSnackbar. We restore the timeout when complete if (parent.isPointInChildBounds(child, (int) event.getX(), (int) event.getY())) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: TBSnackbarManager.getInstance().cancelTimeout(mManagerCallback); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: TBSnackbarManager.getInstance().restoreTimeout(mManagerCallback); break; } } return super.onInterceptTouchEvent(parent, child, event); } } }