Git Product home page Git Product logo

android-actionitembadge's Introduction

Android-ActionItemBadge Maven Central Android Arsenal

Join the chat at https://gitter.im/mikepenz/Android-ActionItemBadge

ActionItemBadge is a library which offers a simple and easy to use method to add a badge to your action item!

Screenshots

Image Image

Include in your project

Using Maven

The ActionItemBadge Library is pushed to [Maven Central], so you just need to add the following dependency to your build.gradle.

dependencies {
	implementation 'com.mikepenz:actionitembadge:4.0.0'

	//SUB-DEPENDENCIES
	//Android-Iconics - used to provide an easy API for icons 
	implementation 'com.mikepenz:iconics-core:{latestVersion}@aar'
}

Additional dependency for the icon font

If you are going to use the icon font you will have to add additional dependency for the font. You can find all available addons here: https://github.com/mikepenz/Android-Iconics#2-choose-your-desired-fonts

UPGRADE NOTES

< 4.0.0

  • If you come from a version prior 4.0.0 you will have to upgrade to AndroidX and Iconics v4

< 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

Create your menu.xml as you would do normally and add the app:actionLayout param. It is also a good idea to set showAsAction="always" (The badge can only be shown in the actionbar)

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/item_samplebadge"
        app:actionLayout="@layout/menu_action_item_badge"
        app:showAsAction="always"
        android:title="@string/sample_1"/>
</menu>

Activity

Override the onCreateOptionsMenu method

 @Override
    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);

	    //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.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 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 super.onCreateOptionsMenu(menu);
    }

If you want to update the item itself you can do the required stuff in the onOptionsItemSelected method and call invalidateOptionsMenu() afterwards.

    @Override
    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();
            badgeCount--;
            ActionItemBadge.update(item, badgeCount);
            return true;
        } else if (id == SAMPLE2_ID) {
            Toast.makeText(this, R.string.sample_4, Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }

Dependencies

Developed By

License

Copyright 2019 Mike Penz

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

android-actionitembadge's People

Contributors

brianjd avatar cedrickflocon avatar danprado avatar gitter-badger avatar mikepenz avatar mohamedmenasy avatar prabel avatar snkashis avatar tral avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

android-actionitembadge's Issues

disappearing badge on toolbar

Hi,
Firstly, thanks for sharing this useful lib.
I have a standard menu file where I am displaying two items set as app:showAsAction="always", one of which is actionitem badge and other is a textview. The value of the badge is updated via (a sticky) async event (greenrobot). That works fine, except when I press the overflow menu the badge disappears, but its place holder remains empty. The text view doesn't have this issue.
Is there anything that i could be missing there?

regards

How to control the padding of the icons generated?

I am using the library as in the following code, and want to control the spacing between icons in actionbar. Is there a way? Thanks

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    Log.d("LIFECYCLE", "OnCreateOptionsMenu() in MainActivity_");
    getMenuInflater().inflate(R.menu.menu_main_actionbar, menu);

    // Set an icon in the ActionBar
    menu.findItem(R.id.action_notification).setIcon(
            new IconDrawable(this, Iconify.IconValue.fa_bell)
                    .colorRes(R.color.white)
                    .actionBarSize());

    menu.findItem(R.id.action_tracking_setup).setIcon(
            new IconDrawable(this, Iconify.IconValue.fa_link)
                    .colorRes(R.color.white)
                    .actionBarSize());


    // set up the notification icon without badge if unreadNotifCount =0, else set the badge


    if(displayedUnreadNotifCount>0){
        ActionItemBadge.update(
                this,
                menu.findItem(R.id.action_notification),
                new IconDrawable(this, Iconify.IconValue.fa_bell).colorRes(R.color.white).actionBarSize(),
                ActionItemBadge.BadgeStyle.YELLOW,
                displayedUnreadNotifCount);
        //unreadNotifCount);
    }
    else{

/* menu.findItem(R.id.action_notification).setIcon(
new IconDrawable(this, Iconify.IconValue.fa_bell)
.colorRes(R.color.white)
.actionBarSize());*/
ActionItemBadge.update(
this,
menu.findItem(R.id.action_notification),
new IconDrawable(this, Iconify.IconValue.fa_bell).colorRes(R.color.white).actionBarSize(),
ActionItemBadge.BadgeStyle.YELLOW,
-1);
}
return true;
}

2015-01-09

Error in syncing

My Gradle File looks like:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.genericapp"
        minSdkVersion 17
        targetSdkVersion 23
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

buildscript {
    repositories {
        jcenter()
    }
}

dependencies {

    repositories {
        maven { url "https://jitpack.io" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }

    //from jar
    compile files('libs/picasso-2.5.2.jar')
    compile files('libs/gson-2.5.jar')

    //from libraries
    compile project(':libraries:PersistentSearch')

    //from online
    compile('com.github.afollestad.material-dialogs:core:0.8.5.3@aar') {
        transitive = true
    }
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.daimajia.slider:library:1.1.5@aar'
    compile 'com.android.support:support-v4:23.1.1'


    compile 'com.mikepenz:actionitembadge:3.2.1@aar'
    //SUB-DEPENDENCIES
    //Android-Iconics - used to provide an easy API for icons
    compile 'com.mikepenz:iconics-core:2.5.4@aar'

}

After syncing grade I am getting this error
Failed to resolve: com.mikepenz:actionitembadge:3.2.1

Set icon as drawable

Right now it's only possible to set the action drawable via Android-Iconify it should be possible to set an image ressource too

com.mikepenz:actionitembadge:3.1.1 null pointer

        Drawable drawable = UIUtil.getCompatDrawable(ScreenActivity.this, R.drawable.icon_sort_white);
        if (drawable == null) {
            Logger.d("null");
            return true;
        }
        ActionItemBadge.update(this, mi_buy, drawable, ActionItemBadge.BadgeStyles.RED, 1);
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackground(android.graphics.drawable.Drawable)' on a null object reference
            at com.mikepenz.actionitembadge.library.utils.UIUtil.setBackground(UIUtil.java:28)
            at com.mikepenz.actionitembadge.library.ActionItemBadge.update(ActionItemBadge.java:200)
            at com.mikepenz.actionitembadge.library.ActionItemBadge.update(ActionItemBadge.java:105)

LayerDrawable Cast Problem?

Im getting cast exception like this "java.lang.ClassCastException: android.graphics.drawable.LayerDrawable cannot be cast to android.graphics.drawable.GradientDrawable" any solution for that? Im using appcompat actionbar

getting null pointer exception

in my menu xml i placed this..

<item android:id="@+id/action_carticon" android:title="@string/action_example"
        app:showAsAction="always"
        android:orderInCategory="100"
        android:actionLayout="@layout/menu_badge"
        />

and in my create options menu i used this

ActionItemBadge.update(this, menu.findItem(R.id.action_carticon), getResources().getDrawable(R.drawable.ic_launcher), ActionItemBadge.BadgeStyle.GREEN,5);

what i done wrong if i create menu item programatically works great.if the count is zero need to hide the item badge.is there any possible.

Can't find class file for com.mikpenz.iconics.typeface.IIcon not found

I used the latest Android studio to try it, but get the following error,

added 2 dependencies

compile 'com.joanzapata.android:android-iconify:1.0.8'
compile 'com.mikepenz.actionitembadge:library:2.0.0@aar'

Error:(48, 28) error: cannot access IIcon
class file for com.mikpenz.iconics.typeface.IIcon not found

update badge

how to update badge on setOnClickListener not in MenuItem

Cannot add badge for style with layout

I meet the drawable parameter problem with this line:

ActionItemBadge.update(this, menu.findItem(R.id.item_samplebadge), Iconify.IconValue.fa_android, ActionItemBadge.BadgeStyle.DARKGREY, badgeCount);

Then I replace Iconify.IconValue.fa_android with new IconDrawable(this, Iconify.IconValue.fa_android).

However, I still face the problem and console shows that:

java.lang.NullPointerException for update method in onCreateOptionsMenu

Same questions

Good day. Thx for library.
I have 3 question.

Why I can't use this library without com.mikepenz:iconics?

Can you create BadgeStyle with circle?

Method 'hide' hide my item too. How can I hide only badge?

ClassCastException

Version 3.2.3, Android 6 (Genymotion)

java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.GradientDrawable
                                                                       at com.mikepenz.actionitembadge.library.utils.BadgeDrawableBuilder.build(BadgeDrawableBuilder.java:39)

Issue regarding to tablayout

I have saw samples that work with actionbar and toolbar, but what about the tablayout ?
Besides, I used viewpager to add fragment.
capture2

on Fragment

I have a trouble use this library on Fragment...
onClick item not work on Fragment

Custom Badge Style

Hi Mike,

I'd like to know if the library provides any methods to define a custom BadgeStyle at least changing colors (without cloning project and adding them manually)

Thanks

Library not working with Maven - proposed solution

Hello,

seems like your pom.xml is missing something to make this library work with maven-android-plugin

specifically, you should change your dependency inside your pom.xml to be:

 <dependency>
      <groupId>com.mikepenz.iconics</groupId>
      <artifactId>library</artifactId>
      <type>aar</type>
      <version>0.7.0</version>
      <scope>compile</scope>
    </dependency>

(i just added the "type" part)
Thanks

Coloring is not working

On Android 5.1 and using ActionItemBadge.BadgeStyle.GREY_LARGE (or any other _LARGE) for styling the color of the menu item can not be changed. I'm using Toolbar with AppCompat v22.

Not working:

Drawable drawable = menu.findItem(R.id.action_chat).getIcon();

drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, color);
// or you can use: drawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY);

if (badgeCount > 0) {
     ActionItemBadge.update(activity, menu.findItem(R.id.action_chat), drawable, ActionItemBadge.BadgeStyle.GREY_LARGE, badgeCount);
     } else {
     ActionItemBadge.hide(menu.findItem(R.id.action_chat));
     }
}

Working:

Drawable drawable = menu.findItem(R.id.action_chat).getIcon();

drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, color);
// or you can use: drawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY);

if (badgeCount > 0) {
     ActionItemBadge.update(activity, menu.findItem(R.id.action_chat), drawable, ActionItemBadge.BadgeStyle.GREY, badgeCount);
     } else {
     ActionItemBadge.hide(menu.findItem(R.id.action_chat));
     }
}

Animation

How can use Animation like Rotating or Flip with ActionItem Menu on Updating the MenuItem such as Count ??

Badge in hamburger icon

I see in some Chinese app, they often add a "red dot" into tab or hamburger icon. For example in this article. In below image, there is a similar "red dot" but not in hamburger.

image

So, is it possible to add such "red dot" into hamburger icon?

getDrawable improve

hi
you use this method UIUtil.getCompatDrawable to avoid deprecated method
you can simply use ResourcesCompat.getDrawable(getResources(),R.drawable.logo,null)
thanks

menu icon not showning

In menu.xml i have set an icon to the menu item. But it doesn't show what i have set. It shows something else.

Make it round

Would it be possible to make the badge a perfect circle when there's only one number? Thanks.

onOptionsItemSelected not called on Fragment

The library binds the onclick event to Activity.onOptionsItemSelected. This results in only the Activity onOptionsItemSelected is called, the Fragments are not.

This event should be dispatched to the Activity.onMenuItemSelected method which properly calls onOptionsItemSelected on both the Activity and its Fragments.

change badge direction from right to left

hi mike. thank you for your another great library.
is there any way to change the badge direction in the MenuItem from right to left ?
i want to use it in a RTL application .

Problem with buildToolsVersion 24.0.0

ActionBar icon with "layout/menu_action_item_badge" added works fine with 23.0.3, but on 24.0.0 it doesn't show up at first (empty square). Only after adding a badge it draws correctly the item.

edit: Looks like I jumped to conclusions too quickly. Looks like it doesn't make any difference which version I use. I added ActionItemBadge.update at the end of onCreateOptionsMenu and seems to work. Without that it's just an empty space.

Long press on action icon does not show tooltip with action name

For "regular" action bar buttons, when I long press the button , a tooltip with action title (android:title from item in menu xml) is shown.

But for action bar buttons with badge (when android:actionLayout="@layout/menu_badge" is used), this behavior is broken - no tooltip is shown on long press.

Doesn't work with support library

I'm using the android.support.v7 library, this means that when I do 'menu.findItem(R.id.item_samplebadge)', I get an object with the 'android.support.v7.internal.view.menu.MenuItemImpl' class. This class doesn't support getActionView() method. This means the library throws the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at com.mikepenz.actionitembadge.library.ActionItemBadge.update(ActionItemBadge.java:199)

Is there anything I can do so it will work with the support library?

Ambiguous Method Call

error

I want to use only BadgeLarge in Toolbar & want to update it's BadgeCount dynamically in my Activity

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.