Skip to content

Display badges to your toolbar menu item easily powered by the latest material design components.

Notifications You must be signed in to change notification settings

AchmadHafid/ToolbarBadgeMenuItem

Repository files navigation

ToolbarBadgeMenuItem

Release API

Assalamu'alaikum brothers and sisters, peace be upon you!

Add badge to your toolbar menu item easily by using this library

image
Download Demo App Here

Main Features

  • Create menu item badge by simply using extension function
  • Support Activity & Fragment
  • Full support for color theming (e.g. light & dark theme from Material Components)

Compatibility

This library is compatible from API 26 & AndroidX.

Download

Add jitpack repository into your root build.gradle

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
    ...
  }
}

Add the dependency

dependencies {
  ...
  implementation 'com.github.AchmadHafid:toolbar-badge-menu-item:4.0.0'
  ...
}

Usage

  1. Set your toolbar menu item action layout like below
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    ...

    <item
        ...
        app:actionLayout="@layout/toolbar_badge_menu_item"
        app:showAsAction="always" />

    ...

</menu>
  1. Override onPrepareOptionsMenu to build badges
override fun onPrepareOptionsMenu(menu: Menu?) = createToolbarBadge(menu) {
    addItem(R.id.item_1, R.drawable.icon_1, totalCount1)
    addItem(R.id.item_2, R.drawable.icon_2) { totalCount2 } // can also use lambda
    withColor {
        /** can also use a plain color resource (e.g. R.color.my_color) */
        textRes       = R.attr.colorSurface
        backgroundRes = R.attr.colorPrimary
        iconTintRes   = R.attr.colorOnSurface // or null (no tint)
    }
}
  1. Optionally You can change the default value for textColorRes, backgroundColorRes and iconTintRes by using extension function available on Application
class MyApp : Application() {

    override fun onCreate() {
        super.onCreate()

        setDefaultToolbarBadgeMenuItemColor {
            textRes       = R.attr.colorSurface   // default value
            backgroundRes = R.attr.colorPrimary   // default value
            iconTintRes   = R.attr.colorOnSurface // default value
        }
    }
}

That's it! May this library ease your Android development task

License

Copyright 2019 Achmad Hafid

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.

About

Display badges to your toolbar menu item easily powered by the latest material design components.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages