Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set text size in Snackbar #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/src/main/java/com/nispok/snackbar/DisplayCompat.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.nispok.snackbar;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;

class DisplayCompat {
Expand Down Expand Up @@ -30,4 +35,20 @@ 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);
}

public static int convertDpToPixels(Context context, int dp){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
int px = (int)TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
return px;
}
}
52 changes: 51 additions & 1 deletion lib/src/main/java/com/nispok/snackbar/Snackbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.*;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
Expand Down Expand Up @@ -90,6 +91,7 @@ public int getLayoutGravity () {
private boolean mShouldDismissOnActionClicked = true;
private EventListener mEventListener;
private Typeface mTextTypeface;
private Float mTextSize = null;
private Typeface mActionTypeface;
private boolean mIsShowing = false;
private boolean mCanSwipeToDismiss = true;
Expand Down Expand Up @@ -467,6 +469,41 @@ public Snackbar textTypeface(Typeface typeface) {
return this;
}

/**
* Use a specific text size for this Snackbar's text
*
* mTextSize = size;
* @return
*/
public Snackbar textSize(float size) {
mTextSize = size;
return this;
}

/**
* Set the default text size to a given unit and value. See {@link
* android.util.TypedValue} for the possible dimension units.
*
* @param unit The desired dimension unit.
* @param size The desired size in the given units.
*
* @attr ref android.R.styleable#TextView_textSize
*/
public Snackbar textSize(int unit, float size) {
Context c = getContext();
Resources r;

if (c == null)
r = Resources.getSystem();
else
r = c.getResources();

textSize(TypedValue.applyDimension(
unit, size, r.getDisplayMetrics()));

return this;
}

/**
* Use a custom typeface for this Snackbar's action label
*
Expand Down Expand Up @@ -536,15 +573,26 @@ private MarginLayoutParams init(Context context, Activity targetActivity, ViewGr
GradientDrawable bg = (GradientDrawable) layout.getBackground();
bg.setColor(mColor);

float fontScaleFactor = 1.0f; final int defaultTextSizeDp = 14;
final int defaultTextSizePx = DisplayCompat.convertDpToPixels(targetActivity, defaultTextSizeDp);
if (mTextSize != null && mTextSize > defaultTextSizePx)
fontScaleFactor = mTextSize/defaultTextSizePx;

params = createMarginLayoutParams(
parent, FrameLayout.LayoutParams.WRAP_CONTENT, dpToPx(mType.getMaxHeight(), scale), mPosition);
parent, FrameLayout.LayoutParams.WRAP_CONTENT,
dpToPx(
mTextSize != null ? (int)(mType.getMaxHeight() * fontScaleFactor ) : mType.getMaxHeight(),
scale),
mPosition);
}

if (mDrawable != mUndefinedDrawable)
setBackgroundDrawable(layout, res.getDrawable(mDrawable));

TextView snackbarText = (TextView) layout.findViewById(R.id.sb__text);
snackbarText.setText(mText);
if (mTextSize != null)
snackbarText.setTextSize(mTextSize);
snackbarText.setTypeface(mTextTypeface);

if (mTextColor != mUndefinedColor) {
Expand All @@ -556,6 +604,8 @@ private MarginLayoutParams init(Context context, Activity targetActivity, ViewGr
TextView snackbarAction = (TextView) layout.findViewById(R.id.sb__action);
if (!TextUtils.isEmpty(mActionLabel)) {
requestLayout();
if (mTextSize != null)
snackbarAction.setTextSize(mTextSize);
snackbarAction.setText(mActionLabel);
snackbarAction.setTypeface(mActionTypeface);

Expand Down
3 changes: 2 additions & 1 deletion lib/src/main/res/layout/sb__template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
android:id="@+id/sb__text"
style="@style/Snackbar.Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:ellipsize="end"
android:layout_toLeftOf="@id/sb__action"
/>
Expand Down