diff --git a/README.md b/README.md index b8374df..dd92eee 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ ActionItemBadge is a library which offers a simple and easy to use method to add a badge to your action item! - ##Screenshots ![Image](https://raw.githubusercontent.com/mikepenz/Android-ActionItemBadge/master/DEV/screenshot/screenshot1_small.png). ![Image](https://raw.githubusercontent.com/mikepenz/Android-ActionItemBadge/master/DEV/screenshot/screenshot2_small.png) @@ -15,7 +14,7 @@ The ActionItemBadge Library is pushed to [Maven Central], so you just need to ad ```javascript dependencies { - compile('com.mikepenz:actionitembadge:2.5.5@aar') { + compile('com.mikepenz:actionitembadge:3.0.0@aar') { transitive = true } } @@ -28,19 +27,9 @@ dependencies { } ``` -You use ActionbarSherlock? No problem there's now a SNAPSHOT release for ActionbarSherlock. Just do the following: - -Add the SNAPSHOT repo to your repositories: -```javascript - maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } -``` - -And the compile dependency -```javascript -dependencies { - compile 'com.tundem.actionitembadge:library-abs:1.2.0@aar' -} -``` +##UPGRADE NOTES +####< 3.0.0 +- If you come from a version prior 3.0.0 you will have to rename some classes, and the default styles also found a new place. Just check out the updated sample app for all the changes. ##Usage ###menu.xml @@ -65,18 +54,19 @@ Override the onCreateOptionsMenu method // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); - //you can add some logic (hide it if the count == 0) + //you can add some logic (hide it if the count == 0) if (badgeCount > 0) { - ActionItemBadge.update(this, menu.findItem(R.id.item_samplebadge), FontAwesome.Icon.faw_android, ActionItemBadge.BadgeStyle.DARKGREY, badgeCount); + ActionItemBadge.update(this, menu.findItem(R.id.item_samplebadge), FontAwesome.Icon.faw_android, ActionItemBadge.BadgeStyles.DARK_GREY, badgeCount); } else { ActionItemBadge.hide(menu.findItem(R.id.item_samplebadge)); } - //If you want to add your ActionItem programmatically you can do this too. You do the following: - new ActionItemBadge.Add().act(this).menu(menu).title(R.string.sample_2).itemDetails(0, SAMPLE2_ID, 1).showAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS).build(ActionItemBadge.BadgeStyle.BLUE_LARGE, 1); + //If you want to add your ActionItem programmatically you can do this too. You do the following: + new ActionItemBadgeAdder().act(this).menu(menu).title(R.string.sample_2).itemDetails(0, SAMPLE2_ID, 1).showAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS).add(bigStyle, 1); return true; } ``` + If you want to update the item itself you can do the required stuff in the onOptionsItemSelected method and call invalidateOptionsMenu() afterwards. ```java @@ -84,9 +74,9 @@ call invalidateOptionsMenu() afterwards. public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.item_samplebadge) { - Toast.makeText(this, R.string.sample_3, Toast.LENGTH_SHORT).show(); + Toast.makeText(this, R.string.sample_3, Toast.LENGTH_SHORT).show();.LENGTH_SHORT).show(); badgeCount--; - invalidateOptionsMenu(); + ActionItemBadge.update(item, badgeCount); return true; } else if (id == SAMPLE2_ID) { Toast.makeText(this, R.string.sample_4, Toast.LENGTH_SHORT).show(); diff --git a/app/build.gradle b/app/build.gradle index 826758b..0102fe2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.mikepenz.actionitembadge.sample" minSdkVersion 14 targetSdkVersion 22 - versionCode 255 - versionName "2.5.5" + versionCode 300 + versionName "3.0.0" } buildTypes { release { @@ -22,10 +22,10 @@ android { dependencies { compile project(':library') - compile('com.mikepenz:materialdrawer:3.0.7@aar') { + compile('com.mikepenz:materialdrawer:3.0.9@aar') { transitive = true } - compile('com.mikepenz:aboutlibraries:5.0.5@aar') { + compile('com.mikepenz:aboutlibraries:5.0.6@aar') { transitive = true } } diff --git a/app/src/main/java/com/mikepenz/actionitembadge/sample/MainActivity.java b/app/src/main/java/com/mikepenz/actionitembadge/sample/MainActivity.java index 0f48f8f..4e6f56f 100644 --- a/app/src/main/java/com/mikepenz/actionitembadge/sample/MainActivity.java +++ b/app/src/main/java/com/mikepenz/actionitembadge/sample/MainActivity.java @@ -12,6 +12,7 @@ import com.mikepenz.aboutlibraries.ui.LibsFragment; import com.mikepenz.actionitembadge.R; import com.mikepenz.actionitembadge.library.ActionItemBadge; +import com.mikepenz.actionitembadge.library.ActionItemBadgeAdder; import com.mikepenz.iconics.typeface.FontAwesome; @@ -45,12 +46,12 @@ public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); if (badgeCount > 0) { - ActionItemBadge.update(this, menu.findItem(R.id.item_samplebadge), FontAwesome.Icon.faw_android, ActionItemBadge.BadgeStyle.DARKGREY, badgeCount); + ActionItemBadge.update(this, menu.findItem(R.id.item_samplebadge), FontAwesome.Icon.faw_android, ActionItemBadge.BadgeStyles.GREY, badgeCount); } else { ActionItemBadge.hide(menu.findItem(R.id.item_samplebadge)); } - new ActionItemBadge.Add().act(this).menu(menu).title(R.string.sample_2).itemDetails(0, SAMPLE2_ID, 1).showAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS).build(ActionItemBadge.BadgeStyle.BLUE_LARGE, 1); + new ActionItemBadgeAdder().act(this).menu(menu).title(R.string.sample_2).itemDetails(0, SAMPLE2_ID, 1).showAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS).add(ActionItemBadge.BadgeStyles.GREY_LARGE, 1); return true; } diff --git a/app/src/main/java/com/mikepenz/actionitembadge/sample/ToolbarActivity.java b/app/src/main/java/com/mikepenz/actionitembadge/sample/ToolbarActivity.java index ff44277..9d4d03c 100644 --- a/app/src/main/java/com/mikepenz/actionitembadge/sample/ToolbarActivity.java +++ b/app/src/main/java/com/mikepenz/actionitembadge/sample/ToolbarActivity.java @@ -1,6 +1,7 @@ package com.mikepenz.actionitembadge.sample; import android.content.Intent; +import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; @@ -15,6 +16,8 @@ import com.mikepenz.aboutlibraries.ui.LibsFragment; import com.mikepenz.actionitembadge.R; import com.mikepenz.actionitembadge.library.ActionItemBadge; +import com.mikepenz.actionitembadge.library.ActionItemBadgeAdder; +import com.mikepenz.actionitembadge.library.utils.BadgeStyle; import com.mikepenz.iconics.typeface.FontAwesome; import com.mikepenz.materialdrawer.Drawer; import com.mikepenz.materialdrawer.DrawerBuilder; @@ -27,8 +30,8 @@ public class ToolbarActivity extends AppCompatActivity { private Drawer drawer; - private ActionItemBadge.BadgeStyle style = ActionItemBadge.BadgeStyle.DARKGREY; - private ActionItemBadge.BadgeStyle bigStyle = ActionItemBadge.BadgeStyle.DARKGREY_LARGE; + private BadgeStyle style = ActionItemBadge.BadgeStyles.DARK_GREY.getStyle(); + private BadgeStyle bigStyle = ActionItemBadge.BadgeStyles.DARK_GREY_LARGE.getStyle(); private int badgeCount = 10; private static final int SAMPLE2_ID = 34536; @@ -55,8 +58,11 @@ protected void onCreate(Bundle savedInstanceState) { new PrimaryDrawerItem().withName("Green").withIdentifier(5), new PrimaryDrawerItem().withName("Purple").withIdentifier(6), new PrimaryDrawerItem().withName("Yellow").withIdentifier(7), + new PrimaryDrawerItem().withName("Custom").withIdentifier(8), new DividerDrawerItem(), - new PrimaryDrawerItem().withName("RESET COUNT").withIdentifier(10) + new PrimaryDrawerItem().withName("SWITCH").withIdentifier(9).withCheckable(false), + new PrimaryDrawerItem().withName("RESET COUNT").withIdentifier(10).withCheckable(false), + new PrimaryDrawerItem().withName("HIDE BADGE").withIdentifier(11).withCheckable(false) ) .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { @Override @@ -66,28 +72,38 @@ public boolean onItemClick(AdapterView adapterView, View view, int i, long l, startActivity(intent); return false; } else if (iDrawerItem.getIdentifier() == 1) { - style = ActionItemBadge.BadgeStyle.DARKGREY; - bigStyle = ActionItemBadge.BadgeStyle.DARKGREY_LARGE; + style = ActionItemBadge.BadgeStyles.DARK_GREY.getStyle(); + bigStyle = ActionItemBadge.BadgeStyles.DARK_GREY_LARGE.getStyle(); } else if (iDrawerItem.getIdentifier() == 2) { - style = ActionItemBadge.BadgeStyle.GREY; - bigStyle = ActionItemBadge.BadgeStyle.GREY_LARGE; + style = ActionItemBadge.BadgeStyles.GREY.getStyle(); + bigStyle = ActionItemBadge.BadgeStyles.GREY_LARGE.getStyle(); } else if (iDrawerItem.getIdentifier() == 3) { - style = ActionItemBadge.BadgeStyle.RED; - bigStyle = ActionItemBadge.BadgeStyle.RED_LARGE; + style = ActionItemBadge.BadgeStyles.RED.getStyle(); + bigStyle = ActionItemBadge.BadgeStyles.RED_LARGE.getStyle(); } else if (iDrawerItem.getIdentifier() == 4) { - style = ActionItemBadge.BadgeStyle.BLUE; - bigStyle = ActionItemBadge.BadgeStyle.BLUE_LARGE; + style = ActionItemBadge.BadgeStyles.BLUE.getStyle(); + bigStyle = ActionItemBadge.BadgeStyles.BLUE_LARGE.getStyle(); } else if (iDrawerItem.getIdentifier() == 5) { - style = ActionItemBadge.BadgeStyle.GREEN; - bigStyle = ActionItemBadge.BadgeStyle.GREEN_LARGE; + style = ActionItemBadge.BadgeStyles.GREEN.getStyle(); + bigStyle = ActionItemBadge.BadgeStyles.GREEN_LARGE.getStyle(); } else if (iDrawerItem.getIdentifier() == 6) { - style = ActionItemBadge.BadgeStyle.PURPLE; - bigStyle = ActionItemBadge.BadgeStyle.PURPLE_LARGE; + style = ActionItemBadge.BadgeStyles.PURPLE.getStyle(); + bigStyle = ActionItemBadge.BadgeStyles.PURPLE_LARGE.getStyle(); } else if (iDrawerItem.getIdentifier() == 7) { - style = ActionItemBadge.BadgeStyle.YELLOW; - bigStyle = ActionItemBadge.BadgeStyle.YELLOW_LARGE; + style = ActionItemBadge.BadgeStyles.YELLOW.getStyle(); + bigStyle = ActionItemBadge.BadgeStyles.YELLOW_LARGE.getStyle(); + } else if (iDrawerItem.getIdentifier() == 8) { + style = new BadgeStyle(BadgeStyle.Style.DEFAULT, R.layout.menu_badge, Color.parseColor("#FE0665"), Color.parseColor("#CC0548"), Color.parseColor("#EEEEEE")); + bigStyle = new BadgeStyle(BadgeStyle.Style.LARGE, R.layout.menu_badge_large, Color.parseColor("#FE0665"), Color.parseColor("#CC0548"), Color.parseColor("#EEEEEE")); + } else if (iDrawerItem.getIdentifier() == 9) { + BadgeStyle temp = style; + style = bigStyle; + bigStyle = temp; + Toast.makeText(ToolbarActivity.this, "No icon provided for the previous large style", Toast.LENGTH_LONG).show(); } else if (iDrawerItem.getIdentifier() == 10) { badgeCount = 10; + } else if (iDrawerItem.getIdentifier() == 11) { + badgeCount = Integer.MIN_VALUE; } invalidateOptionsMenu(); @@ -120,13 +136,13 @@ public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); - if (badgeCount > 0) { - ActionItemBadge.update(this, menu.findItem(R.id.item_samplebadge), FontAwesome.Icon.faw_android, style, badgeCount); - } else { + if (badgeCount == 0) { ActionItemBadge.hide(menu.findItem(R.id.item_samplebadge)); + } else { + ActionItemBadge.update(this, menu.findItem(R.id.item_samplebadge), FontAwesome.Icon.faw_android, style, badgeCount); } - new ActionItemBadge.Add().act(this).menu(menu).title(R.string.sample_2).itemDetails(0, SAMPLE2_ID, 1).showAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS).build(bigStyle, 1); + new ActionItemBadgeAdder().act(this).menu(menu).title(R.string.sample_2).itemDetails(0, SAMPLE2_ID, 1).showAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS).add(bigStyle, 1); return true; } @@ -139,7 +155,7 @@ public boolean onOptionsItemSelected(MenuItem item) { if (id == R.id.item_samplebadge) { Toast.makeText(this, R.string.sample_3, Toast.LENGTH_SHORT).show(); badgeCount--; - invalidateOptionsMenu(); + ActionItemBadge.update(item, badgeCount); return true; } else if (id == SAMPLE2_ID) { Toast.makeText(this, R.string.sample_4, Toast.LENGTH_SHORT).show(); diff --git a/gradle.properties b/gradle.properties index c9f9b35..b88d207 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,8 +17,8 @@ # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true -VERSION_NAME=2.5.5 -VERSION_CODE=255 +VERSION_NAME=3.0.0 +VERSION_CODE=300 GROUP=com.mikepenz POM_DESCRIPTION=Android-ActionItemBadge Library diff --git a/library/build.gradle b/library/build.gradle index 05d8c8b..c4d4541 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 22 - versionCode 255 - versionName "2.5.5" + versionCode 300 + versionName "3.0.0" } buildTypes { release { @@ -20,5 +20,5 @@ android { apply from: 'gradle-mvn-push.gradle' dependencies { - compile 'com.mikepenz:iconics:1.1.0' + compile 'com.mikepenz:iconics:1.2.0' } diff --git a/library/src/main/java/com/mikepenz/actionitembadge/library/ActionItemBadge.java b/library/src/main/java/com/mikepenz/actionitembadge/library/ActionItemBadge.java index 4c70733..d324af6 100644 --- a/library/src/main/java/com/mikepenz/actionitembadge/library/ActionItemBadge.java +++ b/library/src/main/java/com/mikepenz/actionitembadge/library/ActionItemBadge.java @@ -1,15 +1,18 @@ package com.mikepenz.actionitembadge.library; -import android.annotation.SuppressLint; import android.app.Activity; +import android.graphics.Color; import android.graphics.drawable.Drawable; -import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; +import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; +import com.mikepenz.actionitembadge.library.utils.BadgeDrawableBuilder; +import com.mikepenz.actionitembadge.library.utils.BadgeStyle; +import com.mikepenz.actionitembadge.library.utils.UIUtil; import com.mikepenz.iconics.IconicsDrawable; import com.mikepenz.iconics.typeface.IIcon; @@ -17,231 +20,93 @@ * Created by mikepenz on 23.07.14. */ public class ActionItemBadge { - public enum BadgeStyle { - GREY(Style.DEFAULT, R.drawable.menu_grey_badge, R.layout.menu_badge), - DARKGREY(Style.DEFAULT, R.drawable.menu_darkgrey_badge, R.layout.menu_badge), - RED(Style.DEFAULT, R.drawable.menu_red_badge, R.layout.menu_badge), - BLUE(Style.DEFAULT, R.drawable.menu_blue_badge, R.layout.menu_badge), - GREEN(Style.DEFAULT, R.drawable.menu_green_badge, R.layout.menu_badge), - PURPLE(Style.DEFAULT, R.drawable.menu_purple_badge, R.layout.menu_badge), - YELLOW(Style.DEFAULT, R.drawable.menu_yellow_badge, R.layout.menu_badge), - GREY_LARGE(Style.LARGE, R.drawable.menu_grey_badge_large, R.layout.menu_badge_large), - DARKGREY_LARGE(Style.LARGE, R.drawable.menu_darkgrey_badge_large, R.layout.menu_badge_large), - RED_LARGE(Style.LARGE, R.drawable.menu_red_badge_large, R.layout.menu_badge_large), - BLUE_LARGE(Style.LARGE, R.drawable.menu_blue_badge_large, R.layout.menu_badge_large), - GREEN_LARGE(Style.LARGE, R.drawable.menu_green_badge_large, R.layout.menu_badge_large), - PURPLE_LARGE(Style.LARGE, R.drawable.menu_purple_badge_large, R.layout.menu_badge_large), - YELLOW_LARGE(Style.LARGE, R.drawable.menu_yellow_badge_large, R.layout.menu_badge_large); - - private Style style; - private int drawable; - private int layout; - - BadgeStyle(Style style, int drawable, int layout) { + public enum BadgeStyles { + GREY(new BadgeStyle(BadgeStyle.Style.DEFAULT, R.layout.menu_badge, Color.parseColor("#e0e0e0"), Color.parseColor("#c7c7c7"), Color.BLACK)), + DARK_GREY(new BadgeStyle(BadgeStyle.Style.DEFAULT, R.layout.menu_badge, Color.parseColor("#606060"), Color.parseColor("#3e3e3e"), Color.WHITE)), + RED(new BadgeStyle(BadgeStyle.Style.DEFAULT, R.layout.menu_badge, Color.parseColor("#FF4444"), Color.parseColor("#CC0000"), Color.WHITE)), + BLUE(new BadgeStyle(BadgeStyle.Style.DEFAULT, R.layout.menu_badge, Color.parseColor("#33B5E5"), Color.parseColor("#0099CC"), Color.WHITE)), + GREEN(new BadgeStyle(BadgeStyle.Style.DEFAULT, R.layout.menu_badge, Color.parseColor("#99CC00"), Color.parseColor("#669900"), Color.WHITE)), + PURPLE(new BadgeStyle(BadgeStyle.Style.DEFAULT, R.layout.menu_badge, Color.parseColor("#AA66CC"), Color.parseColor("#9933CC"), Color.WHITE)), + YELLOW(new BadgeStyle(BadgeStyle.Style.DEFAULT, R.layout.menu_badge, Color.parseColor("#FFBB33"), Color.parseColor("#FF8800"), Color.WHITE)), + GREY_LARGE(new BadgeStyle(BadgeStyle.Style.LARGE, R.layout.menu_badge_large, Color.parseColor("#e0e0e0"), Color.parseColor("#c7c7c7"), Color.BLACK)), + DARK_GREY_LARGE(new BadgeStyle(BadgeStyle.Style.LARGE, R.layout.menu_badge_large, Color.parseColor("#606060"), Color.parseColor("#3e3e3e"), Color.WHITE)), + RED_LARGE(new BadgeStyle(BadgeStyle.Style.LARGE, R.layout.menu_badge_large, Color.parseColor("#FF4444"), Color.parseColor("#CC0000"), Color.WHITE)), + BLUE_LARGE(new BadgeStyle(BadgeStyle.Style.LARGE, R.layout.menu_badge_large, Color.parseColor("#33B5E5"), Color.parseColor("#0099CC"), Color.WHITE)), + GREEN_LARGE(new BadgeStyle(BadgeStyle.Style.LARGE, R.layout.menu_badge_large, Color.parseColor("#99CC00"), Color.parseColor("#669900"), Color.WHITE)), + PURPLE_LARGE(new BadgeStyle(BadgeStyle.Style.LARGE, R.layout.menu_badge_large, Color.parseColor("#AA66CC"), Color.parseColor("#9933CC"), Color.WHITE)), + YELLOW_LARGE(new BadgeStyle(BadgeStyle.Style.LARGE, R.layout.menu_badge_large, Color.parseColor("#FFBB33"), Color.parseColor("#FF8800"), Color.WHITE)),; + + private BadgeStyle style; + + BadgeStyles(BadgeStyle style) { this.style = style; - this.drawable = drawable; - this.layout = layout; } - public Style getStyle() { + public BadgeStyle getStyle() { return style; } - - public int getDrawable() { - return drawable; - } - - public int getLayout() { - return layout; - } - - - public enum Style { - DEFAULT(1), - LARGE(2); - - private int style; - - Style(int style) { - this.style = style; - } - - public int getStyle() { - return style; - } - } } - public static class Add { - public Add() { - - } - - public Add(Activity activity, Menu menu, String title) { - this.activity = activity; - this.menu = menu; - this.title = title; - } - - private Activity activity; - - public Add act(Activity activity) { - this.activity = activity; - return this; - } - - private Menu menu; - - public Add menu(Menu menu) { - this.menu = menu; - return this; - } - - private String title; - - public Add title(String title) { - this.title = title; - return this; - } - - public Add title(int resId) { - if (activity == null) { - throw new RuntimeException("Activity not set"); - } - - this.title = activity.getString(resId); - return this; - } - - private Integer groupId; - private Integer itemId; - private Integer order; - - public Add itemDetails(int groupId, int itemId, int order) { - this.groupId = groupId; - this.itemId = itemId; - this.order = order; - return this; - } - - private Integer showAsAction; - - public Add showAsAction(int showAsAction) { - this.showAsAction = showAsAction; - return this; - } - - public Menu build(int badgeCount) { - return build((Drawable) null, BadgeStyle.GREY_LARGE, badgeCount); - } - - public Menu build(BadgeStyle style, int badgeCount) { - return build((Drawable) null, style, badgeCount); - } - - public Menu build(IIcon icon, int badgeCount) { - return build(new IconicsDrawable(activity, icon).colorRes(R.color.actionbar_text).actionBarSize(), BadgeStyle.GREY, badgeCount); - } - - public Menu build(Drawable icon, int badgeCount) { - return build(icon, BadgeStyle.GREY, badgeCount); - } - - public Menu build(IIcon icon, BadgeStyle style, int badgeCount) { - return build(new IconicsDrawable(activity, icon).colorRes(R.color.actionbar_text).actionBarSize(), style, badgeCount); - } - - public Menu build(Drawable icon, BadgeStyle style, int badgeCount) { - MenuItem item; - if (groupId != null && itemId != null && order != null) { - item = menu.add(groupId, itemId, order, title); - } else { - item = menu.add(title); - } - - if (showAsAction != null) { - item.setShowAsAction(showAsAction); - } + public static void update(final Activity act, final MenuItem menu, IIcon icon, int badgeCount) { + update(act, menu, new IconicsDrawable(act, icon).color(Color.WHITE).actionBar(), BadgeStyles.DARK_GREY.getStyle(), badgeCount); + } - item.setActionView(style.getLayout()); - update(activity, item, icon, style, badgeCount); - return menu; - } + public static void update(final Activity act, final MenuItem menu, IIcon icon, BadgeStyles style, int badgeCount) { + update(act, menu, icon, style.getStyle(), badgeCount); } - public static void update(final Activity act, final MenuItem menu, int badgeCount) { - update(act, menu, (Drawable) null, null, badgeCount); + public static void update(final Activity act, final MenuItem menu, IIcon icon, BadgeStyle style, int badgeCount) { + update(act, menu, new IconicsDrawable(act, icon).color(Color.WHITE).actionBar(), style, badgeCount); } - public static void update(final Activity act, final MenuItem menu, BadgeStyle style, int badgeCount) { - if (style.getStyle() != BadgeStyle.Style.LARGE) { - throw new RuntimeException("You are not allowed to call update without an icon on a Badge with default style"); - } - update(act, menu, (Drawable) null, style, badgeCount); + public static void update(final Activity act, final MenuItem menu, IIcon icon, int iconColor, int badgeCount) { + update(act, menu, icon, iconColor, BadgeStyles.DARK_GREY.getStyle(), badgeCount); } - public static void update(final Activity act, final MenuItem menu, IIcon icon, int badgeCount) { - update(act, menu, new IconicsDrawable(act, icon).colorRes(R.color.actionbar_text).actionBarSize(), BadgeStyle.GREY, badgeCount); + public static void update(final Activity act, final MenuItem menu, IIcon icon, int iconColor, BadgeStyles style, int badgeCount) { + update(act, menu, icon, iconColor, style.getStyle(), badgeCount); } - public static void update(final Activity act, final MenuItem menu, Drawable icon, int badgeCount) { - update(act, menu, icon, BadgeStyle.GREY, badgeCount); + public static void update(final Activity act, final MenuItem menu, IIcon icon, int iconColor, BadgeStyle style, int badgeCount) { + update(act, menu, new IconicsDrawable(act, icon).color(iconColor).actionBar(), style, badgeCount); } - public static void update(final Activity act, final MenuItem menu, IIcon icon, BadgeStyle style, int badgeCount) { - update(act, menu, new IconicsDrawable(act, icon).colorRes(R.color.actionbar_text).actionBarSize(), style, badgeCount); + public static void update(final Activity act, final MenuItem menu, Drawable icon, BadgeStyles style, int badgeCount) { + update(act, menu, icon, style.getStyle(), badgeCount); } + /** + * update the given menu item with icon and badgeCount and style + * + * @param act + * @param menu + * @param icon + * @param style + * @param badgeCount + */ public static void update(final Activity act, final MenuItem menu, Drawable icon, BadgeStyle style, int badgeCount) { if (menu != null) { - View badge = menu.getActionView(); - - if (style != null) { - if (style.getStyle() == BadgeStyle.Style.DEFAULT) { - ImageView imageView = (ImageView) badge.findViewById(R.id.menu_badge_icon); - if (icon != null) { - ActionItemBadge.setBackground(imageView, icon); - } + menu.setActionView(style.getLayout()); + FrameLayout badge = (FrameLayout) menu.getActionView(); - TextView textView = (TextView) badge.findViewById(R.id.menu_badge_text); - if (badgeCount < 0) { - textView.setVisibility(View.GONE); - } else { - textView.setVisibility(View.VISIBLE); - textView.setText(String.valueOf(badgeCount)); - textView.setBackgroundResource(style.getDrawable()); - } - } else { - Button button = (Button) badge.findViewById(R.id.menu_badge_button); - if (button != null) { - button.setBackgroundResource(style.getDrawable()); - button.setText(String.valueOf(badgeCount)); - } - } - } else { - // i know this is not nice but the best solution to allow doing an update without a style + if (style.getStyle() == BadgeStyle.Style.DEFAULT) { ImageView imageView = (ImageView) badge.findViewById(R.id.menu_badge_icon); - if (imageView != null) { - if (icon != null) { - ActionItemBadge.setBackground(imageView, icon); - } - - TextView textView = (TextView) badge.findViewById(R.id.menu_badge_text); - if (badgeCount < 0) { - textView.setVisibility(View.GONE); - } else { - textView.setVisibility(View.VISIBLE); - textView.setText(String.valueOf(badgeCount)); - } - } else { - Button button = (Button) badge.findViewById(R.id.menu_badge_button); - if (style != null) { - button.setBackgroundResource(style.getDrawable()); - } - button.setText(String.valueOf(badgeCount)); + if (icon != null) { + UIUtil.setBackground(imageView, icon); } } + //get the badgeView. We don't need to check which one we get as a button extends a TextView ;) + TextView badgeView = (TextView) badge.findViewById(R.id.menu_badge); + if (badgeCount == Integer.MIN_VALUE) { + badgeView.setVisibility(View.GONE); + } else { + badgeView.setVisibility(View.VISIBLE); + badgeView.setText(String.valueOf(badgeCount)); + UIUtil.setBackground(badgeView, new BadgeDrawableBuilder().color(style.getColor()).colorPressed(style.getColorPressed()).build(act)); + badgeView.setTextColor(style.getTextColor()); + } + badge.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -253,17 +118,49 @@ public void onClick(View v) { } } - public static void hide(MenuItem menu) { - menu.setVisible(false); + + public static void update(final MenuItem menu, int badgeCount) { + update(menu, null, badgeCount); } - @SuppressLint("NewApi") - private static void setBackground(View v, Drawable d) { - int sdk = android.os.Build.VERSION.SDK_INT; - if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) { - v.setBackgroundDrawable(d); - } else { - v.setBackground(d); + /** + * update the given menu item with icon and badgeCount + * + * @param menu + * @param icon + * @param badgeCount + */ + public static void update(final MenuItem menu, Drawable icon, int badgeCount) { + if (menu != null) { + FrameLayout badge = (FrameLayout) menu.getActionView(); + // i know this is not nice but the best solution to allow doing an update without a style + ImageView imageView = (ImageView) badge.findViewById(R.id.menu_badge_icon); + if (imageView != null) { + if (icon != null) { + UIUtil.setBackground(imageView, icon); + } + + TextView textView = (TextView) badge.findViewById(R.id.menu_badge); + if (badgeCount < 0) { + textView.setVisibility(View.GONE); + } else { + textView.setVisibility(View.VISIBLE); + textView.setText(String.valueOf(badgeCount)); + } + } else { + Button button = (Button) badge.findViewById(R.id.menu_badge); + button.setText(String.valueOf(badgeCount)); + } } } + + + /** + * hide the given menu item + * + * @param menu + */ + public static void hide(MenuItem menu) { + menu.setVisible(false); + } } diff --git a/library/src/main/java/com/mikepenz/actionitembadge/library/ActionItemBadgeAdder.java b/library/src/main/java/com/mikepenz/actionitembadge/library/ActionItemBadgeAdder.java new file mode 100644 index 0000000..75ca7c7 --- /dev/null +++ b/library/src/main/java/com/mikepenz/actionitembadge/library/ActionItemBadgeAdder.java @@ -0,0 +1,128 @@ +package com.mikepenz.actionitembadge.library; + +import android.app.Activity; +import android.graphics.Color; +import android.graphics.drawable.Drawable; +import android.view.Menu; +import android.view.MenuItem; + +import com.mikepenz.actionitembadge.library.utils.BadgeStyle; +import com.mikepenz.iconics.IconicsDrawable; +import com.mikepenz.iconics.typeface.IIcon; + +public class ActionItemBadgeAdder { + public ActionItemBadgeAdder() { + + } + + public ActionItemBadgeAdder(Activity activity, Menu menu, String title) { + this.activity = activity; + this.menu = menu; + this.title = title; + } + + private Activity activity; + + public ActionItemBadgeAdder act(Activity activity) { + this.activity = activity; + return this; + } + + private Menu menu; + + public ActionItemBadgeAdder menu(Menu menu) { + this.menu = menu; + return this; + } + + private String title; + + public ActionItemBadgeAdder title(String title) { + this.title = title; + return this; + } + + public ActionItemBadgeAdder title(int resId) { + if (activity == null) { + throw new RuntimeException("Activity not set"); + } + + this.title = activity.getString(resId); + return this; + } + + private Integer groupId; + private Integer itemId; + private Integer order; + + public ActionItemBadgeAdder itemDetails(int groupId, int itemId, int order) { + this.groupId = groupId; + this.itemId = itemId; + this.order = order; + return this; + } + + private Integer showAsAction; + + public ActionItemBadgeAdder showAsAction(int showAsAction) { + this.showAsAction = showAsAction; + return this; + } + + public Menu add(int badgeCount) { + return add(ActionItemBadge.BadgeStyles.GREY_LARGE, badgeCount); + } + + public Menu add(ActionItemBadge.BadgeStyles style, int badgeCount) { + return add(style.getStyle(), badgeCount); + } + + public Menu add(BadgeStyle style, int badgeCount) { + return add((Drawable) null, style, badgeCount); + } + + public Menu add(IIcon icon, int badgeCount) { + return add(icon, Color.WHITE, badgeCount); + } + + public Menu add(IIcon icon, int iconColor, int badgeCount) { + return add(new IconicsDrawable(activity, icon).color(iconColor).actionBar(), ActionItemBadge.BadgeStyles.GREY, badgeCount); + } + + public Menu add(Drawable icon, int badgeCount) { + return add(icon, ActionItemBadge.BadgeStyles.GREY, badgeCount); + } + + public Menu add(IIcon icon, ActionItemBadge.BadgeStyles style, int badgeCount) { + return add(icon, style.getStyle(), badgeCount); + } + + public Menu add(IIcon icon, BadgeStyle style, int badgeCount) { + return add(icon, Color.WHITE, style, badgeCount); + } + + public Menu add(IIcon icon, int iconColor, BadgeStyle style, int badgeCount) { + return add(new IconicsDrawable(activity, icon).color(iconColor).actionBar(), style, badgeCount); + } + + public Menu add(Drawable icon, ActionItemBadge.BadgeStyles style, int badgeCount) { + return add(icon, style.getStyle(), badgeCount); + } + + public Menu add(Drawable icon, BadgeStyle style, int badgeCount) { + MenuItem item; + if (groupId != null && itemId != null && order != null) { + item = menu.add(groupId, itemId, order, title); + } else { + item = menu.add(title); + } + + if (showAsAction != null) { + item.setShowAsAction(showAsAction); + } + + item.setActionView(style.getLayout()); + ActionItemBadge.update(activity, item, icon, style, badgeCount); + return menu; + } +} \ No newline at end of file diff --git a/library/src/main/java/com/mikepenz/actionitembadge/library/utils/BadgeDrawableBuilder.java b/library/src/main/java/com/mikepenz/actionitembadge/library/utils/BadgeDrawableBuilder.java new file mode 100644 index 0000000..9b75325 --- /dev/null +++ b/library/src/main/java/com/mikepenz/actionitembadge/library/utils/BadgeDrawableBuilder.java @@ -0,0 +1,64 @@ +package com.mikepenz.actionitembadge.library.utils; + +import android.content.Context; +import android.graphics.drawable.GradientDrawable; +import android.graphics.drawable.StateListDrawable; +import android.util.StateSet; + +import com.mikepenz.actionitembadge.library.R; + +/** + * Created by mikepenz on 02.07.15. + */ +public class BadgeDrawableBuilder { + private int mColor = 0; + private int mColorPressed = 0; + private int mCorners = -1; + private int mPadding = -1; + + public BadgeDrawableBuilder() { + } + + public BadgeDrawableBuilder color(int color) { + this.mColor = color; + return this; + } + + public BadgeDrawableBuilder colorPressed(int colorPressed) { + this.mColorPressed = colorPressed; + return this; + } + + public BadgeDrawableBuilder corners(int corners) { + this.mCorners = corners; + return this; + } + + public BadgeDrawableBuilder padding(int padding) { + this.mPadding = padding; + return this; + } + + public StateListDrawable build(Context ctx) { + StateListDrawable stateListDrawable = new StateListDrawable(); + + GradientDrawable normal = (GradientDrawable) UIUtil.getCompatDrawable(ctx, R.drawable.badge); + GradientDrawable selected = (GradientDrawable) normal.getConstantState().newDrawable().mutate(); + + normal.setColor(mColor); + selected.setColor(mColorPressed); + + if (mCorners > -1) { + normal.setCornerRadius(mCorners); + selected.setCornerRadius(mCorners); + } + + if (mPadding > -1) { + } + + stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, selected); + stateListDrawable.addState(StateSet.WILD_CARD, normal); + + return stateListDrawable; + } +} diff --git a/library/src/main/java/com/mikepenz/actionitembadge/library/utils/BadgeStyle.java b/library/src/main/java/com/mikepenz/actionitembadge/library/utils/BadgeStyle.java new file mode 100644 index 0000000..18a8576 --- /dev/null +++ b/library/src/main/java/com/mikepenz/actionitembadge/library/utils/BadgeStyle.java @@ -0,0 +1,84 @@ +package com.mikepenz.actionitembadge.library.utils; + +import android.graphics.Color; + +/** + * Created by mikepenz on 02.07.15. + */ +public class BadgeStyle { + public enum Style { + DEFAULT(1), + LARGE(2); + + private int style; + + Style(int style) { + this.style = style; + } + + public int getStyle() { + return style; + } + } + + private Style style; + private int layout; + private int color; + private int colorPressed; + private int textColor = Color.WHITE; + + public Style getStyle() { + return style; + } + + public void setStyle(Style style) { + this.style = style; + } + + public int getLayout() { + return layout; + } + + public void setLayout(int layout) { + this.layout = layout; + } + + public int getColor() { + return color; + } + + public void setColor(int color) { + this.color = color; + } + + public int getColorPressed() { + return colorPressed; + } + + public void setColorPressed(int colorPressed) { + this.colorPressed = colorPressed; + } + + public int getTextColor() { + return textColor; + } + + public void setTextColor(int textColor) { + this.textColor = textColor; + } + + public BadgeStyle(Style style, int layout, int color, int colorPressed) { + this.style = style; + this.layout = layout; + this.color = color; + this.colorPressed = colorPressed; + } + + public BadgeStyle(Style style, int layout, int color, int colorPressed, int textColor) { + this.style = style; + this.layout = layout; + this.color = color; + this.colorPressed = colorPressed; + this.textColor = textColor; + } +} diff --git a/library/src/main/java/com/mikepenz/actionitembadge/library/utils/UIUtil.java b/library/src/main/java/com/mikepenz/actionitembadge/library/utils/UIUtil.java new file mode 100644 index 0000000..878d99e --- /dev/null +++ b/library/src/main/java/com/mikepenz/actionitembadge/library/utils/UIUtil.java @@ -0,0 +1,49 @@ +package com.mikepenz.actionitembadge.library.utils; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.view.View; + +/** + * Created by mikepenz on 02.07.15. + */ +public class UIUtil { + + /** + * helper method to set the background depending on the android version + * + * @param v + * @param d + */ + @SuppressLint("NewApi") + public static void setBackground(View v, Drawable d) { + if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) { + v.setBackgroundDrawable(d); + } else { + v.setBackground(d); + } + } + + + /** + * helper method to get the drawable by its resource. specific to the correct android version + * + * @param c + * @param drawableRes + * @return + */ + public static Drawable getCompatDrawable(Context c, int drawableRes) { + Drawable d = null; + try { + if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + d = c.getResources().getDrawable(drawableRes); + } else { + d = c.getResources().getDrawable(drawableRes, c.getTheme()); + } + } catch (Exception ex) { + } + return d; + } +} diff --git a/library/src/main/res/drawable/badge.xml b/library/src/main/res/drawable/badge.xml new file mode 100644 index 0000000..a517591 --- /dev/null +++ b/library/src/main/res/drawable/badge.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_blue_badge.xml b/library/src/main/res/drawable/menu_blue_badge.xml deleted file mode 100644 index 598320c..0000000 --- a/library/src/main/res/drawable/menu_blue_badge.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_blue_badge_large.xml b/library/src/main/res/drawable/menu_blue_badge_large.xml deleted file mode 100644 index 4299785..0000000 --- a/library/src/main/res/drawable/menu_blue_badge_large.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_darkgrey_badge.xml b/library/src/main/res/drawable/menu_darkgrey_badge.xml deleted file mode 100644 index 93be4b8..0000000 --- a/library/src/main/res/drawable/menu_darkgrey_badge.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_darkgrey_badge_large.xml b/library/src/main/res/drawable/menu_darkgrey_badge_large.xml deleted file mode 100644 index b803351..0000000 --- a/library/src/main/res/drawable/menu_darkgrey_badge_large.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_green_badge.xml b/library/src/main/res/drawable/menu_green_badge.xml deleted file mode 100644 index dc0d89a..0000000 --- a/library/src/main/res/drawable/menu_green_badge.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_green_badge_large.xml b/library/src/main/res/drawable/menu_green_badge_large.xml deleted file mode 100644 index 84a74b3..0000000 --- a/library/src/main/res/drawable/menu_green_badge_large.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_grey_badge.xml b/library/src/main/res/drawable/menu_grey_badge.xml deleted file mode 100644 index 1d8cc68..0000000 --- a/library/src/main/res/drawable/menu_grey_badge.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_grey_badge_large.xml b/library/src/main/res/drawable/menu_grey_badge_large.xml deleted file mode 100644 index 1e20954..0000000 --- a/library/src/main/res/drawable/menu_grey_badge_large.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_purple_badge.xml b/library/src/main/res/drawable/menu_purple_badge.xml deleted file mode 100644 index 13a4c71..0000000 --- a/library/src/main/res/drawable/menu_purple_badge.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_purple_badge_large.xml b/library/src/main/res/drawable/menu_purple_badge_large.xml deleted file mode 100644 index baf722c..0000000 --- a/library/src/main/res/drawable/menu_purple_badge_large.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_red_badge.xml b/library/src/main/res/drawable/menu_red_badge.xml deleted file mode 100644 index 157db13..0000000 --- a/library/src/main/res/drawable/menu_red_badge.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_red_badge_large.xml b/library/src/main/res/drawable/menu_red_badge_large.xml deleted file mode 100644 index 8935860..0000000 --- a/library/src/main/res/drawable/menu_red_badge_large.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_yellow_badge.xml b/library/src/main/res/drawable/menu_yellow_badge.xml deleted file mode 100644 index 6fc25e2..0000000 --- a/library/src/main/res/drawable/menu_yellow_badge.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/drawable/menu_yellow_badge_large.xml b/library/src/main/res/drawable/menu_yellow_badge_large.xml deleted file mode 100644 index 8c3ebb9..0000000 --- a/library/src/main/res/drawable/menu_yellow_badge_large.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/library/src/main/res/layout/menu_badge.xml b/library/src/main/res/layout/menu_badge.xml index 9d4ddd7..d48e656 100644 --- a/library/src/main/res/layout/menu_badge.xml +++ b/library/src/main/res/layout/menu_badge.xml @@ -1,31 +1,28 @@ + style="?android:attr/actionButtonStyle" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:clickable="true" + android:focusable="true" + android:gravity="center"> + android:layout_width="match_parent" + android:layout_height="match_parent" /> + android:textSize="12sp" /> \ No newline at end of file diff --git a/library/src/main/res/layout/menu_badge_large.xml b/library/src/main/res/layout/menu_badge_large.xml index 7bf64cc..5132931 100644 --- a/library/src/main/res/layout/menu_badge_large.xml +++ b/library/src/main/res/layout/menu_badge_large.xml @@ -1,19 +1,19 @@ - + + - \ No newline at end of file + android:singleLine="true" + android:textColor="@android:color/white" + android:textSize="16sp" /> + \ No newline at end of file diff --git a/library/src/main/res/values/aboutlibraries.xml b/library/src/main/res/values/aboutlibraries.xml index 3de13c5..a5ae09a 100644 --- a/library/src/main/res/values/aboutlibraries.xml +++ b/library/src/main/res/values/aboutlibraries.xml @@ -9,7 +9,7 @@ Android-ActionItemBadge is a library which offers a simple and easy to use method to add a badge to your action item!! ]]> - 2.5.5 + 3.0.0 https://github.com/mikepenz/Android-ActionItemBadge apache_2_0 true diff --git a/library/src/main/res/values/colors.xml b/library/src/main/res/values/colors.xml deleted file mode 100644 index 825b5c2..0000000 --- a/library/src/main/res/values/colors.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - #ffffff" - - #FF4444 - #CC0000 - - #33B5E5 - #0099CC - - #AA66CC - #9933CC - - #99CC00 - #669900 - - #FFBB33 - #FF8800 - - #e0e0e0 - #c7c7c7 - - #606060 - #3e3e3e - \ No newline at end of file