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;
}
Aggregations