Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ bottomNavigation.setAccentColor(Color.parseColor("#F63D2B"));
bottomNavigation.setInactiveColor(Color.parseColor("#747474"));

// Force to tint the drawable (useful for font with icon for example)
// false : ignore tint color, apply selected icon
// true : apply tint color, ignore selected icon
bottomNavigation.setForceTint(true);

// Display color under navigation bar (API 21+)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,17 @@ private void createClassicItems(LinearLayout linearLayout) {
setBackgroundColor(defaultBackgroundColor);
}
}


if (forceTint) {
icon.setImageDrawable(AHHelper.getTintDrawable(items.get(i).getDrawable(context),
current ? itemActiveColor : itemInactiveColor, forceTint));
} else {
if (current) {
icon.setImageDrawable(AHHelper.getTintDrawable(items.get(i).getSelectedDrawable(context)));
}
}

title.setTextColor(current ? itemActiveColor : itemInactiveColor);
title.setTextSize(TypedValue.COMPLEX_UNIT_PX, current ? activeSize : inactiveSize);

if (itemsEnabledStates[i]) {
Expand Down Expand Up @@ -516,7 +526,7 @@ private void createSmallItems(LinearLayout linearLayout) {


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

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

Expand All @@ -538,7 +548,7 @@ private void createSmallItems(LinearLayout linearLayout) {
title.setTypeface(titleTypeface);
}

if (i == currentItem) {
if (current) {
if (selectedBackgroundVisible) {
view.setSelected(true);
}
Expand Down Expand Up @@ -567,7 +577,7 @@ private void createSmallItems(LinearLayout linearLayout) {
}

if (colored) {
if (i == currentItem) {
if (current) {
setBackgroundColor(item.getColor(context));
currentColor = item.getColor(context);
}
Expand All @@ -579,6 +589,27 @@ private void createSmallItems(LinearLayout linearLayout) {
}
}

if (forceTint) {
icon.setImageDrawable(AHHelper.getTintDrawable(items.get(i).getDrawable(context),
current ? itemActiveColor : itemInactiveColor, forceTint));
} else {
if (current) {
icon.setImageDrawable(AHHelper.getTintDrawable(items.get(i).getSelectedDrawable(context)));
}
}

title.setTextColor(current ? itemActiveColor : itemInactiveColor);
title.setAlpha(current ? 1 : 0);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
updateSmallItems(itemIndex, true);
}
});
view.setSoundEffectsEnabled(soundEffectsEnabled);

int width = current ? (int) selectedItemWidth :

if (itemsEnabledStates[i]) {
icon.setImageDrawable(AHHelper.getTintDrawable(items.get(i).getDrawable(context),
currentItem == i ? itemActiveColor : itemInactiveColor, forceTint));
Expand All @@ -598,8 +629,7 @@ public void onClick(View v) {
title.setAlpha(0);
}

int width = i == currentItem ? (int) selectedItemWidth :
(int) itemWidth;
int width = i == currentItem ? (int) selectedItemWidth : (int) itemWidth;

if (titleState == TitleState.ALWAYS_HIDE) {
width = (int) (itemWidth * 1.16);
Expand Down Expand Up @@ -665,8 +695,12 @@ private void updateItems(final int itemIndex, boolean useCallback) {
AHHelper.updateLeftMargin(notification, notificationInactiveMarginLeft, notificationActiveMarginLeft);
AHHelper.updateTextColor(title, itemInactiveColor, itemActiveColor);
AHHelper.updateTextSize(title, inactiveSize, activeSize);
AHHelper.updateDrawableColor(context, items.get(itemIndex).getDrawable(context), icon,
itemInactiveColor, itemActiveColor, forceTint);
if (forceTint) {
AHHelper.updateDrawableColor(context, items.get(itemIndex).getDrawable(context), icon,
itemInactiveColor, itemActiveColor, forceTint);
} else {
AHHelper.updateDrawable(context, items.get(itemIndex).getSelectedDrawable(context), icon);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && colored) {

Expand Down Expand Up @@ -726,8 +760,12 @@ public void onAnimationRepeat(Animator animation) {
AHHelper.updateLeftMargin(notification, notificationActiveMarginLeft, notificationInactiveMarginLeft);
AHHelper.updateTextColor(title, itemActiveColor, itemInactiveColor);
AHHelper.updateTextSize(title, activeSize, inactiveSize);
AHHelper.updateDrawableColor(context, items.get(currentItem).getDrawable(context), icon,
itemActiveColor, itemInactiveColor, forceTint);
if (forceTint) {
AHHelper.updateDrawableColor(context, items.get(currentItem).getDrawable(context), icon,
itemActiveColor, itemInactiveColor, forceTint);
} else {
AHHelper.updateDrawable(context, items.get(currentItem).getDrawable(context), icon);
}
}
}

Expand Down Expand Up @@ -792,8 +830,12 @@ private void updateSmallItems(final int itemIndex, boolean useCallback) {
}

AHHelper.updateAlpha(title, 0, 1);
AHHelper.updateDrawableColor(context, items.get(itemIndex).getDrawable(context), icon,
itemInactiveColor, itemActiveColor, forceTint);
if (forceTint) {
AHHelper.updateDrawableColor(context, items.get(itemIndex).getDrawable(context), icon,
itemInactiveColor, itemActiveColor, forceTint);
} else {
AHHelper.updateDrawable(context, items.get(itemIndex).getSelectedDrawable(context), icon);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && colored) {
int finalRadius = Math.max(getWidth(), getHeight());
Expand Down Expand Up @@ -859,8 +901,12 @@ public void onAnimationRepeat(Animator animation) {
}

AHHelper.updateAlpha(title, 1, 0);
AHHelper.updateDrawableColor(context, items.get(currentItem).getDrawable(context), icon,
itemActiveColor, itemInactiveColor, forceTint);
if (forceTint) {
AHHelper.updateDrawableColor(context, items.get(currentItem).getDrawable(context), icon,
itemActiveColor, itemInactiveColor, forceTint);
} else {
AHHelper.updateDrawable(context, items.get(currentItem).getDrawable(context), icon);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class AHBottomNavigationItem {

private String title = "";
private Drawable drawable;
private Drawable selectedDrawable;
private int color = Color.GRAY;

private
Expand All @@ -27,6 +28,8 @@ public class AHBottomNavigationItem {
private
@DrawableRes
int drawableRes = 0;
@DrawableRes
int selectedDrawableRes = 0;
private
@ColorRes
int colorRes = 0;
Expand All @@ -53,7 +56,15 @@ public AHBottomNavigationItem(String title, @DrawableRes int resource, @ColorRes
this.drawableRes = resource;
this.color = color;
}


@Deprecated
public AHBottomNavigationItem(String title, @DrawableRes int resource, @DrawableRes int selectedResource, @ColorRes int color) {
this.title = title;
this.drawableRes = resource;
this.selectedDrawableRes = selectedResource;
this.color = color;
}

/**
* Constructor
*
Expand All @@ -66,7 +77,14 @@ public AHBottomNavigationItem(@StringRes int titleRes, @DrawableRes int drawable
this.drawableRes = drawableRes;
this.colorRes = colorRes;
}


public AHBottomNavigationItem(@StringRes int titleRes, @DrawableRes int drawableRes, @DrawableRes int selectedDrawableRes, @ColorRes int colorRes) {
this.titleRes = titleRes;
this.drawableRes = drawableRes;
this.selectedDrawableRes = selectedDrawableRes;
this.colorRes = colorRes;
}

/**
* Constructor
*
Expand All @@ -90,7 +108,14 @@ public AHBottomNavigationItem(String title, Drawable drawable, @ColorInt int col
this.drawable = drawable;
this.color = color;
}


public AHBottomNavigationItem(String title, Drawable drawable, Drawable selectedDrawable, @ColorInt int color) {
this.title = title;
this.drawable = drawable;
this.selectedDrawable = selectedDrawable;
this.color = color;
}

public String getTitle(Context context) {
if (titleRes != 0) {
return context.getString(titleRes);
Expand Down Expand Up @@ -128,14 +153,31 @@ public void setColorRes(@ColorRes int colorRes) {
public Drawable getDrawable(Context context) {
if (drawableRes != 0) {
try {
return VectorDrawableCompat.create(context.getResources(), drawableRes, null);
}catch (Resources.NotFoundException e){
return AppCompatResources.getDrawable(context, drawableRes);
} catch (Resources.NotFoundException e) {
return ContextCompat.getDrawable(context, drawableRes);
}
}
return drawable;
}


public Drawable getSelectedDrawable(Context context) {
if (selectedDrawableRes != 0) {
try {
return VectorDrawableCompat.create(context.getResources(), selectedDrawableRes, null);
}catch (Resources.NotFoundException e){
return ContextCompat.getDrawable(context, selectedDrawableRes);
}
}

if (selectedDrawable != null)
return selectedDrawable;

return getDrawable(context);
}

public void setDrawable(@DrawableRes int drawableRes) {
this.drawableRes = drawableRes;
this.drawable = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public static Drawable getTintDrawable(Drawable drawable, @ColorInt int color, b
return wrapDrawable;
}

public static Drawable getTintDrawable(Drawable drawable) {
Drawable wrapDrawable = DrawableCompat.wrap(drawable).mutate();
return wrapDrawable;
}

/**
* Update top margin with animation
*/
Expand Down Expand Up @@ -187,6 +192,11 @@ public void onAnimationUpdate(ValueAnimator animator) {
colorAnimation.start();
}

public static void updateDrawable(final Context context, final Drawable drawable, final ImageView imageView) {
imageView.setImageDrawable(AHHelper.getTintDrawable(drawable));
imageView.requestLayout();
}

/**
* Update width
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class DemoActivity extends AppCompatActivity {
private DemoViewPagerAdapter adapter;
private AHBottomNavigationAdapter navigationAdapter;
private ArrayList<AHBottomNavigationItem> bottomNavigationItems = new ArrayList<>();
private boolean useMenuResource = true;
private boolean useMenuResource = false;
private int[] tabColors;
private Handler handler = new Handler();

Expand Down Expand Up @@ -64,6 +64,8 @@ private void initUI() {
}

bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);
bottomNavigation.setForceTint(false); // false : ignore tint color, apply selected icon
// true : apply tint color, ignore selected icon
viewPager = (AHBottomNavigationViewPager) findViewById(R.id.view_pager);
floatingActionButton = (FloatingActionButton) findViewById(R.id.floating_action_button);

Expand All @@ -72,9 +74,9 @@ private void initUI() {
navigationAdapter = new AHBottomNavigationAdapter(this, R.menu.bottom_navigation_menu_3);
navigationAdapter.setupWithBottomNavigation(bottomNavigation, tabColors);
} else {
AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.tab_1, R.drawable.ic_apps_black_24dp, R.color.color_tab_1);
AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.tab_2, R.drawable.ic_maps_local_bar, R.color.color_tab_2);
AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.tab_3, R.drawable.ic_maps_local_restaurant, R.color.color_tab_3);
AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.tab_1, R.drawable.map, R.drawable.map_active, R.color.color_tab_1);
AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.tab_2, R.drawable.calc, R.drawable.calc_active, R.color.color_tab_2);
AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.tab_3, R.drawable.settings, R.drawable.settings_active, R.color.color_tab_3);

bottomNavigationItems.add(item1);
bottomNavigationItems.add(item2);
Expand Down
Binary file added demo/src/main/res/drawable-hdpi/calc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/main/res/drawable-hdpi/calc_active.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/main/res/drawable-hdpi/map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/main/res/drawable-hdpi/map_active.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/main/res/drawable-hdpi/settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.