Skip to content

Commit

Permalink
Stack buttons if insufficient room to display them side by side
Browse files Browse the repository at this point in the history
  • Loading branch information
sergivonavi committed Apr 21, 2019
1 parent cf27096 commit e66c766
Show file tree
Hide file tree
Showing 2 changed files with 286 additions and 63 deletions.
68 changes: 21 additions & 47 deletions library/src/main/java/com/sergivonavi/materialbanner/Banner.java
Expand Up @@ -35,7 +35,6 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.google.android.material.button.MaterialButton;
Expand Down Expand Up @@ -98,6 +97,7 @@
* </p>
*/
public class Banner extends ViewGroup implements BannerInterface {
private static final String TAG = "Banner";
private static final boolean DEBUG = false;

@IntDef(value = {VISIBLE, INVISIBLE, GONE})
Expand Down Expand Up @@ -133,9 +133,6 @@ public class Banner extends ViewGroup implements BannerInterface {
private int mMessageMarginBottomMultiline;
private int mMessageMarginBottomWithIcon;

private int mButtonMarginEnd;
private int mButtonMarginBottom;

private int mLineHeight;

/**
Expand Down Expand Up @@ -191,9 +188,6 @@ private void loadDimens(Context context) {
mMessageMarginBottomMultiline = getDimen(R.dimen.mb_message_margin_bottom_multiline);
mMessageMarginBottomWithIcon = getDimen(R.dimen.mb_message_margin_bottom_with_icon);

mButtonMarginEnd = getDimen(R.dimen.mb_button_margin_end);
mButtonMarginBottom = getDimen(R.dimen.mb_button_margin_bottom);

mLineHeight = getDimen(R.dimen.mb_line_height);

mContainerPaddingTopOneLine = getDimen(R.dimen.mb_container_padding_top_singleline);
Expand Down Expand Up @@ -248,40 +242,10 @@ private void initViewGroup(Context context) {

mButtonsContainer = new ButtonsContainer(context);
mButtonsContainer.setId(R.id.mb_container_buttons);
mButtonsContainer.setOrientation(LinearLayout.HORIZONTAL);
mButtonsContainer.setBaselineAligned(false);
mButtonsContainer.setLayoutParams(relativeLayoutParams);

// BUTTONS' PARAMS
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(WRAP_CONTENT,
WRAP_CONTENT);
linearLayoutParams.weight = 1.0f;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
linearLayoutParams.setMarginEnd(mButtonMarginEnd);
} else {
linearLayoutParams.rightMargin = mButtonMarginEnd;
}
linearLayoutParams.bottomMargin = mButtonMarginBottom;

// LEFT BUTTON
mLeftButton = new MaterialButton(context, null, R.attr.borderlessButtonStyle);
mLeftButton.setId(R.id.mb_button_left);
mLeftButton.setSingleLine(true);
mLeftButton.setMaxLines(1);
mLeftButton.setMinWidth(0);
mLeftButton.setMinimumWidth(0);
mLeftButton.setLayoutParams(linearLayoutParams);
mLeftButton.setVisibility(GONE);

// RIGHT BUTTON
mRightButton = new MaterialButton(context, null, R.attr.borderlessButtonStyle);
mRightButton.setId(R.id.mb_button_right);
mRightButton.setSingleLine(true);
mRightButton.setMaxLines(1);
mRightButton.setMinWidth(0);
mRightButton.setMinimumWidth(0);
mRightButton.setLayoutParams(linearLayoutParams);
mRightButton.setVisibility(GONE);
mLeftButton = mButtonsContainer.getLeftButton();
mRightButton = mButtonsContainer.getRightButton();

// LINE
layoutParams = new LayoutParams(MATCH_PARENT, mLineHeight);
Expand All @@ -293,9 +257,6 @@ private void initViewGroup(Context context) {
addView(mContentContainer);
addView(mLine);

mButtonsContainer.addView(mLeftButton, 0);
mButtonsContainer.addView(mRightButton, 1);

mContentContainer.addView(mIconView);
mContentContainer.addView(mMessageView);
mContentContainer.addView(mButtonsContainer);
Expand Down Expand Up @@ -457,13 +418,26 @@ private void onMultiline() {
.getLayoutParams();

if (mWideLayout) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
messageLayoutParams.addRule(START_OF, mButtonsContainer.getId());
if (mButtonsContainer.getMeasuredWidth()
> (getMeasuredWidth() - getContainerHorizontalPadding()) / 2) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
messageLayoutParams.addRule(START_OF, 0);
} else {
messageLayoutParams.addRule(LEFT_OF, 0);
}
messageLayoutParams.bottomMargin = mIcon
== null ? mMessageMarginBottomMultiline : mMessageMarginBottomWithIcon;

buttonsContainerLayoutParams.addRule(BELOW, mMessageView.getId());
} else {
messageLayoutParams.addRule(LEFT_OF, mButtonsContainer.getId());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
messageLayoutParams.addRule(START_OF, mButtonsContainer.getId());
} else {
messageLayoutParams.addRule(LEFT_OF, mButtonsContainer.getId());
}

buttonsContainerLayoutParams.addRule(ALIGN_BASELINE, mMessageView.getId());
buttonsContainerLayoutParams.addRule(ALIGN_BASELINE, mMessageView.getId());
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
messageLayoutParams.addRule(START_OF, 0);
Expand Down

0 comments on commit e66c766

Please sign in to comment.