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

Custom features #309

Open
wants to merge 4 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import android.os.Parcelable;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.Px;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
Expand Down Expand Up @@ -110,10 +112,14 @@ public enum TitleState {
private float selectedItemWidth, notSelectedItemWidth;
private boolean forceTint = false;
private TitleState titleState = TitleState.SHOW_WHEN_ACTIVE;
private boolean showDividers = false;
private @ColorInt int dividerColor;
private @Px int dividerWidth;

// Notifications
private @ColorInt int notificationTextColor;
private @ColorInt int notificationBackgroundColor;
private @DrawableRes int notificationBackgroundResId;
private Drawable notificationBackgroundDrawable;
private Typeface notificationTypeface;
private int notificationActiveMarginLeft, notificationInactiveMarginLeft;
Expand Down Expand Up @@ -395,6 +401,17 @@ private void createClassicItems(LinearLayout linearLayout) {
}

for (int i = 0; i < items.size(); i++) {

if (showDividers) {
if (i > 0) {
View separator = new View(getContext());
separator.setBackgroundColor(dividerColor);

LayoutParams params = new LayoutParams(dividerWidth, (int) height);
linearLayout.addView(separator, params);
}
}

final boolean current = currentItem == i;
final int itemIndex = i;
AHBottomNavigationItem item = items.get(itemIndex);
Expand All @@ -408,6 +425,8 @@ private void createClassicItems(LinearLayout linearLayout) {
icon.setImageDrawable(item.getDrawable(context));
title.setText(item.getTitle(context));

AHHelper.updateIconSize(item, icon, resources);

if (titleTypeface != null) {
title.setTypeface(titleTypeface);
}
Expand Down Expand Up @@ -517,6 +536,16 @@ private void createSmallItems(LinearLayout linearLayout) {

for (int i = 0; i < items.size(); i++) {

if (showDividers) {
if (i > 0) {
View separator = new View(getContext());
separator.setBackgroundColor(dividerColor);

LayoutParams params = new LayoutParams(dividerWidth, (int) height);
linearLayout.addView(separator, params);
}
}

final int itemIndex = i;
AHBottomNavigationItem item = items.get(itemIndex);

Expand All @@ -526,6 +555,8 @@ private void createSmallItems(LinearLayout linearLayout) {
TextView notification = (TextView) view.findViewById(R.id.bottom_navigation_notification);
icon.setImageDrawable(item.getDrawable(context));

AHHelper.updateIconSize(item, icon, resources);

if (titleState != TitleState.ALWAYS_HIDE) {
title.setText(item.getTitle(context));
}
Expand Down Expand Up @@ -895,6 +926,7 @@ private void updateNotifications(boolean updateStyle, int itemPosition) {
final AHNotification notificationItem = notifications.get(i);
final int currentTextColor = AHNotificationHelper.getTextColor(notificationItem, notificationTextColor);
final int currentBackgroundColor = AHNotificationHelper.getBackgroundColor(notificationItem, notificationBackgroundColor);
final Drawable currentBackgroundDrawable = AHNotificationHelper.getBackgroundDrawable(getContext(), notificationItem, notificationBackgroundResId);

TextView notification = (TextView) views.get(i).findViewById(R.id.bottom_navigation_notification);

Expand All @@ -909,22 +941,30 @@ private void updateNotifications(boolean updateStyle, int itemPosition) {
notification.setTypeface(null, Typeface.BOLD);
}

if (notificationBackgroundDrawable != null) {
if (currentBackgroundDrawable != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
Drawable drawable = notificationBackgroundDrawable.getConstantState().newDrawable();
notification.setBackground(drawable);
notification.setBackground(currentBackgroundDrawable);
} else {
notification.setBackgroundDrawable(notificationBackgroundDrawable);
notification.setBackgroundDrawable(currentBackgroundDrawable);
}

} else if (currentBackgroundColor != 0) {
Drawable defautlDrawable = ContextCompat.getDrawable(context, R.drawable.notification_background);
} else if (notificationBackgroundDrawable != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.setBackground(AHHelper.getTintDrawable(defautlDrawable,
currentBackgroundColor, forceTint));
Drawable drawable = notificationBackgroundDrawable.getConstantState().newDrawable();
notification.setBackground(drawable);
} else {
notification.setBackgroundDrawable(AHHelper.getTintDrawable(defautlDrawable,
currentBackgroundColor, forceTint));
notification.setBackgroundDrawable(notificationBackgroundDrawable);
}
} else {
if (currentBackgroundColor != 0) {
Drawable defautlDrawable = ContextCompat.getDrawable(context, R.drawable.notification_background);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.setBackground(AHHelper.getTintDrawable(defautlDrawable,
currentBackgroundColor, forceTint));
} else {
notification.setBackgroundDrawable(AHHelper.getTintDrawable(defautlDrawable,
currentBackgroundColor, forceTint));
}
}
}
}
Expand Down Expand Up @@ -1366,6 +1406,40 @@ public void setTitleState(TitleState titleState) {
createItems();
}

/**
* Return the current state of the divider shown flag
*
* @return true if dividers are enabled
*/
public boolean isShowDividers() {
return showDividers;
}

/**
* Add dividers separating each item with default configuration (color and width)
*
* @param showDividers true if dividers should be enabled
*/
public void showDividers(boolean showDividers) {
showDividers(showDividers, R.color.colorBottomNavigationDivider, R.dimen.bottom_navigation_divider_width);
}

/**
* Add dividers separating each item with custom configuration (color and width)
*
* @param showDividers true if dividers should be enabled
* @param colorRes resource for the divider color
* @param widthRes resource for the divider width
*/
public void showDividers(boolean showDividers, @ColorRes int colorRes, @DimenRes int widthRes) {
this.showDividers = showDividers;
if (showDividers) {
this.dividerColor = ContextCompat.getColor(getContext(), colorRes);
this.dividerWidth = resources.getDimensionPixelSize(widthRes);
}
createItems();
}

/**
* Set AHOnTabSelectedListener
*/
Expand Down Expand Up @@ -1430,6 +1504,20 @@ public void setNotification(String title, int itemPosition) {
updateNotifications(false, itemPosition);
}

/**
* Set notification text
*
* @param iconResId int
* @param itemPosition int
*/
public void setNotificationIcon(@DrawableRes int iconResId, int itemPosition) {
if (itemPosition < 0 || itemPosition > items.size() - 1) {
throw new IndexOutOfBoundsException(String.format(Locale.US, EXCEPTION_INDEX_OUT_OF_BOUNDS, itemPosition, items.size()));
}
notifications.set(itemPosition, AHNotification.justIcon(iconResId));
updateNotifications(true, itemPosition);
}

/**
* Set fully customized Notification
*
Expand Down Expand Up @@ -1477,6 +1565,17 @@ public void setNotificationBackground(Drawable drawable) {
updateNotifications(true, UPDATE_ALL_NOTIFICATIONS);
}

/**
* Set notification background resource
*
* @param drawableResId int
*/
public void setNotificationBackgroundResource(@DrawableRes int drawableResId) {
this.notificationBackgroundDrawable = ContextCompat.getDrawable(getContext(), drawableResId);
updateNotifications(true, UPDATE_ALL_NOTIFICATIONS);
}


/**
* Set notification background color
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.content.res.AppCompatResources;
Expand All @@ -30,6 +32,9 @@ public class AHBottomNavigationItem {
private
@ColorRes
int colorRes = 0;

// optional
private @Nullable IconSize iconSize;

/**
* Constructor
Expand Down Expand Up @@ -145,4 +150,33 @@ public void setDrawable(Drawable drawable) {
this.drawable = drawable;
this.drawableRes = 0;
}

public AHBottomNavigationItem setIconSize(@Nullable IconSize iconSize) {
this.iconSize = iconSize;
return this;
}

@Nullable
public IconSize getIconSize() {
return iconSize;
}

public static class IconSize {

private final @DimenRes int widthRes;
private final @DimenRes int heightRes;

public IconSize(int widthRes, int heightRes) {
this.widthRes = widthRes;
this.heightRes = heightRes;
}

public int getWidthRes() {
return widthRes;
}

public int getHeightRes() {
return heightRes;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
Expand Down Expand Up @@ -256,4 +257,17 @@ public static Activity unwrap(Context context) {
}
return (Activity) context;
}

/**
* Update icon size if requested
*/
public static void updateIconSize(AHBottomNavigationItem item, ImageView icon, Resources res) {
AHBottomNavigationItem.IconSize iconSize = item.getIconSize();
if (iconSize != null) {
ViewGroup.LayoutParams iconParams = icon.getLayoutParams();
iconParams.width = res.getDimensionPixelSize(iconSize.getWidthRes());
iconParams.height = res.getDimensionPixelSize(iconSize.getHeightRes());
icon.setLayoutParams(iconParams);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.text.TextUtils;

Expand All @@ -23,6 +24,9 @@ public class AHNotification implements Parcelable {
@ColorInt
private int backgroundColor; // if 0 then use default value

@DrawableRes
private int backgroundResId; // if 0 then use default value

public AHNotification() {
// empty
}
Expand All @@ -31,6 +35,7 @@ private AHNotification(Parcel in) {
text = in.readString();
textColor = in.readInt();
backgroundColor = in.readInt();
backgroundResId = in.readInt();
}

public boolean isEmpty() {
Expand All @@ -49,10 +54,18 @@ public int getBackgroundColor() {
return backgroundColor;
}

public int getBackgroundResId() {
return backgroundResId;
}

public static AHNotification justText(String text) {
return new Builder().setText(text).build();
}

public static AHNotification justIcon(@DrawableRes int iconResId) {
return new Builder().setBackgroundColor(0).setBackgroundResId(iconResId).setText(" ").build();
}

public static List<AHNotification> generateEmptyList(int size) {
List<AHNotification> notificationList = new ArrayList<>();
for (int i = 0; i < size; i++) {
Expand All @@ -71,6 +84,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(text);
dest.writeInt(textColor);
dest.writeInt(backgroundColor);
dest.writeInt(backgroundResId);
}

public static class Builder {
Expand All @@ -80,6 +94,8 @@ public static class Builder {
private int textColor;
@ColorInt
private int backgroundColor;
@DrawableRes
private int backgroundResId;

public Builder setText(String text) {
this.text = text;
Expand All @@ -96,11 +112,17 @@ public Builder setBackgroundColor(@ColorInt int backgroundColor) {
return this;
}

public Builder setBackgroundResId(@DrawableRes int backgroundResId) {
this.backgroundResId = backgroundResId;
return this;
}

public AHNotification build() {
AHNotification notification = new AHNotification();
notification.text = text;
notification.textColor = textColor;
notification.backgroundColor = backgroundColor;
notification.backgroundResId = backgroundResId;
return notification;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.aurelhubert.ahbottomnavigation.notification;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;

/**
* @author repitch
Expand Down Expand Up @@ -36,4 +40,17 @@ public static int getBackgroundColor(@NonNull AHNotification notification, @Colo
return backgroundColor == 0 ? defaultBackgroundColor : backgroundColor;
}

/**
* Get background drawable for given notification. If drawable is not set (0), returns default value.
*
* @param notification AHNotification, non null
* @param defaultBackgroundResId int default background resource for all notifications
* @return
*/
public static Drawable getBackgroundDrawable(@NonNull Context context, @NonNull AHNotification notification, @DrawableRes int defaultBackgroundResId) {
int backgroundResId = notification.getBackgroundResId();
return backgroundResId != 0 ?
ContextCompat.getDrawable(context, backgroundResId) :
defaultBackgroundResId != 0 ? ContextCompat.getDrawable(context, defaultBackgroundResId) : null;
}
}
Loading