Skip to content
Browse files

Improve AHBottomNavigationItem

  • Loading branch information...
1 parent 3fb27ef commit 13a6bd27f46b5404011e2dcb3c8e12e71a416d37 @aurelhubert committed
View
6 ahbottomnavigation/build.gradle
@@ -13,7 +13,7 @@ ext {
siteUrl = 'https://github.com/aurelhubert/ahbottomnavigation'
gitUrl = 'https://github.com/aurelhubert/ahbottomnavigation.git'
- libraryVersion = '0.1.3'
+ libraryVersion = '0.1.4'
developerId = 'aurelhubert'
developerName = 'Aurelien Hubert'
@@ -31,8 +31,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
- versionCode 3
- versionName "0.1.3"
+ versionCode 4
+ versionName "0.1.4"
}
buildTypes {
release {
View
51 ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigation.java
@@ -4,6 +4,7 @@
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
+import android.support.annotation.ColorInt;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
@@ -190,8 +191,8 @@ private void createClassicItems(LinearLayout linearLayout) {
View view = inflater.inflate(R.layout.bottom_navigation_item, this, false);
ImageView icon = (ImageView) view.findViewById(R.id.bottom_navigation_item_icon);
TextView title = (TextView) view.findViewById(R.id.bottom_navigation_item_title);
- icon.setImageResource(item.getResource());
- title.setText(item.getTitle());
+ icon.setImageDrawable(item.getDrawable(context));
+ title.setText(item.getTitle(context));
if (i == currentItem) {
int activePaddingTop = (int) context.getResources()
@@ -202,11 +203,11 @@ private void createClassicItems(LinearLayout linearLayout) {
if (colored) {
if (i == currentItem) {
- setBackgroundColor(item.getColor());
- currentColor = item.getColor();
+ setBackgroundColor(item.getColor(context));
+ currentColor = item.getColor(context);
}
- icon.setImageDrawable(AHHelper.getTintDrawable(context, items.get(i).getResource(),
+ icon.setImageDrawable(AHHelper.getTintDrawable(context, items.get(i).getDrawable(context),
currentItem == i ? ContextCompat.getColor(context, R.color.colorActiveSmall) :
ContextCompat.getColor(context, R.color.colorInactiveSmall)));
title.setTextColor(currentItem == i ?
@@ -215,7 +216,7 @@ private void createClassicItems(LinearLayout linearLayout) {
} else {
setBackgroundColor(defaultBackgroundColor);
- icon.setImageDrawable(AHHelper.getTintDrawable(context, items.get(i).getResource(),
+ icon.setImageDrawable(AHHelper.getTintDrawable(context, items.get(i).getDrawable(context),
currentItem == i ? accentColor : inactiveColor));
title.setTextColor(currentItem == i ? accentColor : inactiveColor);
}
@@ -276,8 +277,8 @@ private void createSmallItems(LinearLayout linearLayout) {
View view = inflater.inflate(R.layout.bottom_navigation_small_item, this, false);
ImageView icon = (ImageView) view.findViewById(R.id.bottom_navigation_small_item_icon);
TextView title = (TextView) view.findViewById(R.id.bottom_navigation_small_item_title);
- icon.setImageResource(item.getResource());
- title.setText(item.getTitle());
+ icon.setImageDrawable(item.getDrawable(context));
+ title.setText(item.getTitle(context));
if (i == currentItem) {
int activePaddingTop = (int) context.getResources()
@@ -290,11 +291,11 @@ private void createSmallItems(LinearLayout linearLayout) {
if (colored) {
if (i == currentItem) {
- setBackgroundColor(item.getColor());
- currentColor = item.getColor();
+ setBackgroundColor(item.getColor(context));
+ currentColor = item.getColor(context);
}
- icon.setImageDrawable(AHHelper.getTintDrawable(context, items.get(i).getResource(),
+ icon.setImageDrawable(AHHelper.getTintDrawable(context, items.get(i).getDrawable(context),
currentItem == i ? ContextCompat.getColor(context, R.color.colorActiveSmall) :
ContextCompat.getColor(context, R.color.colorInactiveSmall)));
title.setTextColor(currentItem == i ?
@@ -302,7 +303,7 @@ private void createSmallItems(LinearLayout linearLayout) {
ContextCompat.getColor(context, R.color.colorInactiveSmall));
} else {
setBackgroundColor(defaultBackgroundColor);
- icon.setImageDrawable(AHHelper.getTintDrawable(context, items.get(i).getResource(),
+ icon.setImageDrawable(AHHelper.getTintDrawable(context, items.get(i).getDrawable(context),
currentItem == i ? accentColor : inactiveColor));
title.setTextColor(currentItem == i ? accentColor : inactiveColor);
}
@@ -355,11 +356,11 @@ private void updateItems(final int itemIndex) {
AHHelper.updateTopPadding(container, inactivePaddingTop, activePaddingTop);
AHHelper.updateTextColor(title, itemInactiveColor, itemActiveColor);
AHHelper.updateTextSize(title, inactiveSize, activeSize);
- AHHelper.updateDrawableColor(context, items.get(itemIndex).getResource(), icon,
+ AHHelper.updateDrawableColor(context, items.get(itemIndex).getDrawable(context), icon,
itemInactiveColor, itemActiveColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && colored) {
- backgroundColorView.setBackgroundColor(items.get(itemIndex).getColor());
+ backgroundColorView.setBackgroundColor(items.get(itemIndex).getColor(context));
int finalRadius = Math.max(getWidth(), getHeight());
int cx = (int) views.get(itemIndex).getX() + views.get(itemIndex).getWidth() / 2;
@@ -372,7 +373,7 @@ public void onAnimationStart(Animator animation) {
@Override
public void onAnimationEnd(Animator animation) {
- setBackgroundColor(items.get(itemIndex).getColor());
+ setBackgroundColor(items.get(itemIndex).getColor(context));
}
@Override
@@ -386,7 +387,7 @@ public void onAnimationRepeat(Animator animation) {
anim.start();
} else if (colored) {
AHHelper.updateViewBackgroundColor(this, currentColor,
- items.get(itemIndex).getColor());
+ items.get(itemIndex).getColor(context));
} else {
setBackgroundColor(defaultBackgroundColor);
}
@@ -400,13 +401,13 @@ public void onAnimationRepeat(Animator animation) {
AHHelper.updateTopPadding(container, activePaddingTop, inactivePaddingTop);
AHHelper.updateTextColor(title, itemActiveColor, itemInactiveColor);
AHHelper.updateTextSize(title, activeSize, inactiveSize);
- AHHelper.updateDrawableColor(context, items.get(currentItem).getResource(), icon,
+ AHHelper.updateDrawableColor(context, items.get(currentItem).getDrawable(context), icon,
itemActiveColor, itemInactiveColor);
}
}
currentItem = itemIndex;
- currentColor = items.get(currentItem).getColor();
+ currentColor = items.get(currentItem).getColor(context);
if (listener != null) {
listener.onTabSelected(itemIndex);
@@ -447,11 +448,11 @@ private void updateSmallItems(final int itemIndex) {
AHHelper.updateTextColor(title, itemInactiveColor, itemActiveColor);
AHHelper.updateAlpha(title, 0, 1);
AHHelper.updateWidth(container, notSelectedItemWidth, selectedItemWidth);
- AHHelper.updateDrawableColor(context, items.get(itemIndex).getResource(), icon,
+ AHHelper.updateDrawableColor(context, items.get(itemIndex).getDrawable(context), icon,
itemInactiveColor, itemActiveColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && colored) {
- backgroundColorView.setBackgroundColor(items.get(itemIndex).getColor());
+ backgroundColorView.setBackgroundColor(items.get(itemIndex).getColor(context));
int finalRadius = Math.max(getWidth(), getHeight());
int cx = (int) views.get(itemIndex).getX() + views.get(itemIndex).getWidth() / 2;
int cy = views.get(itemIndex).getHeight() / 2;
@@ -463,7 +464,7 @@ public void onAnimationStart(Animator animation) {
@Override
public void onAnimationEnd(Animator animation) {
- setBackgroundColor(items.get(itemIndex).getColor());
+ setBackgroundColor(items.get(itemIndex).getColor(context));
}
@Override
@@ -477,7 +478,7 @@ public void onAnimationRepeat(Animator animation) {
anim.start();
} else if (colored) {
AHHelper.updateViewBackgroundColor(this, currentColor,
- items.get(itemIndex).getColor());
+ items.get(itemIndex).getColor(context));
} else {
setBackgroundColor(defaultBackgroundColor);
}
@@ -492,13 +493,13 @@ public void onAnimationRepeat(Animator animation) {
AHHelper.updateTextColor(title, itemActiveColor, itemInactiveColor);
AHHelper.updateAlpha(title, 1, 0);
AHHelper.updateWidth(container, selectedItemWidth, notSelectedItemWidth);
- AHHelper.updateDrawableColor(context, items.get(currentItem).getResource(), icon,
+ AHHelper.updateDrawableColor(context, items.get(currentItem).getDrawable(context), icon,
itemActiveColor, itemInactiveColor);
}
}
currentItem = itemIndex;
- currentColor = items.get(currentItem).getColor();
+ currentColor = items.get(currentItem).getColor(context);
if (listener != null) {
listener.onTabSelected(itemIndex);
@@ -591,7 +592,7 @@ public int getDefaultBackgroundColor() {
*
* @param defaultBackgroundColor The bottom navigation background color
*/
- public void setDefaultBackgroundColor(int defaultBackgroundColor) {
+ public void setDefaultBackgroundColor(@ColorInt int defaultBackgroundColor) {
this.defaultBackgroundColor = defaultBackgroundColor;
createItems();
}
View
91 ...omnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHBottomNavigationItem.java
@@ -1,6 +1,13 @@
package com.aurelhubert.ahbottomnavigation;
+import android.content.Context;
import android.graphics.Color;
+import android.graphics.drawable.Drawable;
+import android.support.annotation.ColorInt;
+import android.support.annotation.ColorRes;
+import android.support.annotation.DrawableRes;
+import android.support.annotation.StringRes;
+import android.support.v4.content.ContextCompat;
/**
* AHBottomNavigationItem
@@ -9,8 +16,18 @@
public class AHBottomNavigationItem {
private String title = "";
+ private Drawable drawable;
private int color = Color.GRAY;
- private int resource = 0;
+
+ private
+ @StringRes
+ int titleRes = 0;
+ private
+ @DrawableRes
+ int drawableRes = 0;
+ private
+ @ColorRes
+ int colorRes = 0;
/**
* Constructor
@@ -26,7 +43,7 @@ public AHBottomNavigationItem() {
*/
public AHBottomNavigationItem(String title, int resource) {
this.title = title;
- this.resource = resource;
+ this.drawableRes = resource;
}
/**
@@ -34,33 +51,87 @@ public AHBottomNavigationItem(String title, int resource) {
* @param resource Drawable resource
* @param color Background color
*/
+ @Deprecated
public AHBottomNavigationItem(String title, int resource, int color) {
this.title = title;
- this.resource = resource;
+ this.drawableRes = resource;
this.color = color;
}
- public String getTitle() {
+ /**
+ * Constructor
+ *
+ * @param titleRes String resource
+ * @param drawableRes Drawable resource
+ * @param colorRes Color resource
+ */
+ public AHBottomNavigationItem(@StringRes int titleRes, @DrawableRes int drawableRes, @ColorRes int colorRes) {
+ this.titleRes = titleRes;
+ this.drawableRes = drawableRes;
+ this.colorRes = colorRes;
+ }
+
+ /**
+ * Constructor
+ *
+ * @param title String
+ * @param drawable Drawable
+ * @param color Color
+ */
+ public AHBottomNavigationItem(String title, Drawable drawable, @ColorInt int color) {
+ this.title = title;
+ this.drawable = drawable;
+ this.color = color;
+ }
+
+ public String getTitle(Context context) {
+ if (titleRes != 0) {
+ return context.getString(titleRes);
+ }
return title;
}
public void setTitle(String title) {
this.title = title;
+ this.titleRes = 0;
+ }
+
+ public void setTitle(@StringRes int titleRes) {
+ this.titleRes = titleRes;
+ this.title = "";
}
- public int getColor() {
+ public int getColor(Context context) {
+ if (colorRes != 0) {
+ return ContextCompat.getColor(context, colorRes);
+ }
return color;
}
- public void setColor(int color) {
+ public void setColor(@ColorInt int color) {
this.color = color;
+ this.colorRes = 0;
+ }
+
+ public void setColorRes(@ColorRes int colorRes) {
+ this.colorRes = colorRes;
+ this.color = 0;
+ }
+
+ public Drawable getDrawable(Context context) {
+ if (drawableRes != 0) {
+ return ContextCompat.getDrawable(context, drawableRes);
+ }
+ return drawable;
}
- public int getResource() {
- return resource;
+ public void setDrawable(@DrawableRes int drawableRes) {
+ this.drawableRes = drawableRes;
+ this.drawable = null;
}
- public void setResource(int resource) {
- this.resource = resource;
+ public void setDrawable(Drawable drawable) {
+ this.drawable = drawable;
+ this.drawableRes = 0;
}
}
View
10 ahbottomnavigation/src/main/java/com/aurelhubert/ahbottomnavigation/AHHelper.java
@@ -7,7 +7,6 @@
import android.content.ContextWrapper;
import android.graphics.drawable.Drawable;
import android.os.Build;
-import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.DisplayMetrics;
import android.util.TypedValue;
@@ -27,13 +26,12 @@
* Return a tint drawable
*
* @param context
- * @param drawableResource
+ * @param drawable
* @param color
* @return
*/
- public static Drawable getTintDrawable(Context context, int drawableResource, int color) {
- Drawable normalDrawable = ContextCompat.getDrawable(context, drawableResource);
- Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
+ public static Drawable getTintDrawable(Context context, Drawable drawable, int color) {
+ Drawable wrapDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(wrapDrawable, color);
return wrapDrawable;
}
@@ -122,7 +120,7 @@ public void onAnimationUpdate(ValueAnimator animator) {
/**
* Update image view color with animation
*/
- public static void updateDrawableColor(final Context context, final int drawable,
+ public static void updateDrawableColor(final Context context, final Drawable drawable,
final ImageView imageView, int fromColor, int toColor) {
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), fromColor, toColor);
colorAnimation.setDuration(150);
View
16 demo/src/main/java/aurelhubert/com/ahbottomnavigation/DemoActivity.java
@@ -3,6 +3,7 @@
import android.app.FragmentManager;
import android.graphics.Color;
import android.os.Bundle;
+import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
@@ -31,9 +32,9 @@ private void initUI() {
bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);
- AHBottomNavigationItem item1 = new AHBottomNavigationItem("Label One", R.drawable.ic_maps_place, Color.parseColor("#455C65"));
- AHBottomNavigationItem item2 = new AHBottomNavigationItem("Label Two", R.drawable.ic_maps_local_bar, Color.parseColor("#00886A"));
- AHBottomNavigationItem item3 = new AHBottomNavigationItem("Label Three", R.drawable.ic_maps_local_restaurant, Color.parseColor("#8B6B62"));
+ AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.tab_1, R.drawable.ic_maps_place, 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);
bottomNavigationItems.add(item1);
bottomNavigationItems.add(item2);
@@ -82,8 +83,13 @@ public boolean isBottomNavigationColored() {
*/
public void updateBottomNavigationItems(boolean addItems) {
- AHBottomNavigationItem item4 = new AHBottomNavigationItem("Label Four", R.drawable.ic_maps_local_bar, Color.parseColor("#6C4A42"));
- AHBottomNavigationItem item5 = new AHBottomNavigationItem("Label Five", R.drawable.ic_maps_place, Color.parseColor("#F63D2B"));
+
+ AHBottomNavigationItem item4 = new AHBottomNavigationItem(getString(R.string.tab_4),
+ ContextCompat.getDrawable(this, R.drawable.ic_maps_local_bar),
+ ContextCompat.getColor(this, R.color.color_tab_5));
+ AHBottomNavigationItem item5 = new AHBottomNavigationItem(getString(R.string.tab_5),
+ ContextCompat.getDrawable(this, R.drawable.ic_maps_place),
+ ContextCompat.getColor(this, R.color.color_tab_5));
if (addItems) {
bottomNavigation.addItem(item4);
View
1 demo/src/main/res/values-v19/styles.xml
@@ -6,6 +6,7 @@
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
+ <item name="colorControlHighlight">@color/ripple_material_dark</item>
</style>
</resources>
View
7 demo/src/main/res/values/colors.xml
@@ -6,4 +6,11 @@
<color name="colorInactive">#747474</color>
<color name="colorActiveSmall">#FFFFFF</color>
<color name="colorInactiveSmall">#50FFFFFF</color>
+
+ <color name="color_tab_1">#455C65</color>
+ <color name="color_tab_2">#00886A</color>
+ <color name="color_tab_3">#8B6B62</color>
+ <color name="color_tab_4">#6C4A42</color>
+ <color name="color_tab_5">#F63D2B</color>
+
</resources>
View
6 demo/src/main/res/values/strings.xml
@@ -1,3 +1,9 @@
<resources>
<string name="app_name">AHBottomNavigation</string>
+ <string name="tab_1">Label 1</string>
+ <string name="tab_2">Label 2</string>
+ <string name="tab_3">Label 3</string>
+ <string name="tab_4">Label 4</string>
+ <string name="tab_5">Label 5</string>
+
</resources>
View
1 demo/src/main/res/values/styles.xml
@@ -6,6 +6,7 @@
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
+ <item name="colorControlHighlight">@color/ripple_material_dark</item>
</style>
</resources>

0 comments on commit 13a6bd2

Please sign in to comment.
Something went wrong with that request. Please try again.