Search in sources :

Example 1 with SnackbarLayout

use of com.nispok.snackbar.layouts.SnackbarLayout in project snackbar by nispok.

the class Snackbar method init.

private MarginLayoutParams init(Context context, Activity targetActivity, ViewGroup parent, boolean usePhoneLayout) {
    SnackbarLayout layout = (SnackbarLayout) LayoutInflater.from(context).inflate(R.layout.sb__template, this, true);
    layout.setOrientation(LinearLayout.VERTICAL);
    Resources res = getResources();
    mColor = mColor != mUndefinedColor ? mColor : res.getColor(R.color.sb__background);
    mOffset = res.getDimensionPixelOffset(R.dimen.sb__offset);
    mUsePhoneLayout = usePhoneLayout;
    float scale = res.getDisplayMetrics().density;
    View divider = layout.findViewById(R.id.sb__divider);
    MarginLayoutParams params;
    if (mUsePhoneLayout) {
        // Phone
        layout.setMinimumHeight(dpToPx(mType.getMinHeight(), scale));
        layout.setMaxHeight(dpToPx(mType.getMaxHeight(), scale));
        layout.setBackgroundColor(mColor);
        params = createMarginLayoutParams(parent, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, mPhonePosition);
        if (mLineColor != null) {
            divider.setBackgroundColor(mLineColor);
        } else {
            divider.setVisibility(View.GONE);
        }
    } else {
        // Tablet/desktop
        // Force single-line
        mType = SnackbarType.SINGLE_LINE;
        layout.setMinimumWidth(res.getDimensionPixelSize(R.dimen.sb__min_width));
        layout.setMaxWidth(mMaxWidthPercentage == null ? res.getDimensionPixelSize(R.dimen.sb__max_width) : DisplayCompat.getWidthFromPercentage(targetActivity, mMaxWidthPercentage));
        layout.setBackgroundResource(R.drawable.sb__bg);
        GradientDrawable bg = (GradientDrawable) layout.getBackground();
        bg.setColor(mColor);
        params = createMarginLayoutParams(parent, FrameLayout.LayoutParams.WRAP_CONTENT, dpToPx(mType.getMaxHeight(), scale), mWidePosition);
        if (mLineColor != null) {
            divider.setBackgroundResource(R.drawable.sb__divider_bg);
            GradientDrawable dbg = (GradientDrawable) divider.getBackground();
            dbg.setColor(mLineColor);
        } else {
            divider.setVisibility(View.GONE);
        }
    }
    if (mDrawable != mUndefinedDrawable)
        setBackgroundDrawable(layout, res.getDrawable(mDrawable));
    snackbarText = (TextView) layout.findViewById(R.id.sb__text);
    snackbarText.setText(mText);
    snackbarText.setTypeface(mTextTypeface);
    if (mTextColor != mUndefinedColor) {
        snackbarText.setTextColor(mTextColor);
    }
    snackbarText.setMaxLines(mType.getMaxLines());
    snackbarAction = (TextView) layout.findViewById(R.id.sb__action);
    if (!TextUtils.isEmpty(mActionLabel)) {
        requestLayout();
        snackbarAction.setText(mActionLabel);
        snackbarAction.setTypeface(mActionTypeface);
        if (mActionColor != mUndefinedColor) {
            snackbarAction.setTextColor(mActionColor);
        }
        snackbarAction.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                if (mActionClickListener != null) {
                    // 2) If we aren't allowing multiple clicks, that this is the first click
                    if (!mIsDismissing && (!mActionClicked || mShouldAllowMultipleActionClicks)) {
                        mActionClickListener.onActionClicked(Snackbar.this);
                        mActionClicked = true;
                    }
                }
                if (mShouldDismissOnActionClicked) {
                    dismiss();
                }
            }
        });
        snackbarAction.setMaxLines(mType.getMaxLines());
    } else {
        snackbarAction.setVisibility(GONE);
    }
    View inner = layout.findViewById(R.id.sb__inner);
    inner.setClickable(true);
    if (mCanSwipeToDismiss && res.getBoolean(R.bool.sb__is_swipeable)) {
        inner.setOnTouchListener(new SwipeDismissTouchListener(this, null, new SwipeDismissTouchListener.DismissCallbacks() {

            @Override
            public boolean canDismiss(Object token) {
                return true;
            }

            @Override
            public void onDismiss(View view, Object token) {
                if (view != null) {
                    if (mActionSwipeListener != null) {
                        mActionSwipeListener.onSwipeToDismiss();
                    }
                    dismiss(false);
                }
            }

            @Override
            public void pauseTimer(boolean shouldPause) {
                if (isIndefiniteDuration()) {
                    return;
                }
                if (shouldPause) {
                    removeCallbacks(mDismissRunnable);
                    mSnackbarFinish = System.currentTimeMillis();
                } else {
                    mTimeRemaining -= (mSnackbarFinish - mSnackbarStart);
                    startTimer(mTimeRemaining);
                }
            }
        }));
    }
    return params;
}
Also used : SwipeDismissTouchListener(com.nispok.snackbar.listeners.SwipeDismissTouchListener) Resources(android.content.res.Resources) SnackbarLayout(com.nispok.snackbar.layouts.SnackbarLayout) View(android.view.View) AbsListView(android.widget.AbsListView) TextView(android.widget.TextView) GradientDrawable(android.graphics.drawable.GradientDrawable)

Aggregations

Resources (android.content.res.Resources)1 GradientDrawable (android.graphics.drawable.GradientDrawable)1 View (android.view.View)1 AbsListView (android.widget.AbsListView)1 TextView (android.widget.TextView)1 SnackbarLayout (com.nispok.snackbar.layouts.SnackbarLayout)1 SwipeDismissTouchListener (com.nispok.snackbar.listeners.SwipeDismissTouchListener)1