Skip to content

Commit

Permalink
Merge pull request #91 from kosiara/feature/add.max.width.percentage.…
Browse files Browse the repository at this point in the history
…support

.maxWidthPercentage(float width) method
  • Loading branch information
wmora committed Mar 6, 2015
2 parents f1e24ec + d03bd78 commit 03c6df6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/src/main/java/com/nispok/snackbar/DisplayCompat.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nispok.snackbar;

import android.app.Activity;
import android.graphics.Point;
import android.os.Build;
import android.view.Display;
Expand Down Expand Up @@ -30,4 +31,12 @@ public static void getSize(Display display, Point outSize) {
public static void getRealSize(Display display, Point outSize) {
IMPL.getRealSize(display, outSize);
}

public static int getWidthFromPercentage(Activity targetActivity, Float mMaxWidthPercentage) {
Display display = targetActivity.getWindowManager().getDefaultDisplay();
Point dispSize = new Point();
getRealSize(display, dispSize);

return (int) (dispSize.x * mMaxWidthPercentage);
}
}
11 changes: 10 additions & 1 deletion lib/src/main/java/com/nispok/snackbar/Snackbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public int getLayoutGravity() {
private Point mDisplaySize = new Point();
private Point mRealDisplaySize = new Point();
private Activity mTargetActivity;
private Float mMaxWidthPercentage = null;
private boolean mUsePhoneLayout;
private Runnable mDismissRunnable = new Runnable() {
@Override
Expand Down Expand Up @@ -546,7 +547,10 @@ private MarginLayoutParams init(Context context, Activity targetActivity, ViewGr
// Tablet/desktop
mType = SnackbarType.SINGLE_LINE; // Force single-line
layout.setMinimumWidth(res.getDimensionPixelSize(R.dimen.sb__min_width));
layout.setMaxWidth(res.getDimensionPixelSize(R.dimen.sb__max_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);
Expand Down Expand Up @@ -740,6 +744,11 @@ public void show(ViewGroup parent, boolean usePhoneLayout) {
showInternal(null, params, parent);
}

public Snackbar maxWidthPercentage(float maxWidthPercentage) {
mMaxWidthPercentage = maxWidthPercentage;
return this;
}

private void showInternal(Activity targetActivity, MarginLayoutParams params, ViewGroup parent) {
parent.removeView(this);

Expand Down

0 comments on commit 03c6df6

Please sign in to comment.