My question is the same as this question (which is not a duplicate of this question).
The only answer to that question does not work for me as, rather than changing the default hamburger icon to the left of the activity's title, it just adds an additional hamburger icon to the right of my activity's title.
So how do I actually get this:
I've been poking around at it all day, but have got nowhere.
I see that Toolbar
has a setNavigationIcon(Drawable drawable)
method. Ideally, I would like to use a layout
(that contains the hamburger icon and the badge view) instead of a Drawable
, but I'm not sure if/how this is achievable - or if there is a better way?
NB - This isn't a question about how to create the badge view. I have already created that and have implemented it on the nav menu items themselves. So I am now just needing to add a similar badge view to the default hamburger icon.
Since version 24.2.0 of the support library, the v7 version of
ActionBarDrawerToggle
has offered thesetDrawerArrowDrawable()
method as a means to customize the toggle icon.DrawerArrowDrawable
is the class that provides that default icon, and it can be subclassed to alter it as needed.As an example, the
BadgeDrawerArrowDrawable
class overrides thedraw()
method to add a basic red and white badge after the superclass draws itself. This allows the hamburger-arrow animation to be preserved underneath.An instance of this can be set on the toggle any time after it's instantiated, and the badge's properties set directly on the drawable as needed.
As the OP noted below, the
Context
used for the customDrawerArrowDrawable
should be obtained withActionBar#getThemedContext()
orToolbar#getContext()
to ensure the correct style values are used. For example:To simplify things a bit, it might be preferable to subclass
ActionBarDrawerToggle
as well, and handle everything through the toggle instance.With this, the custom badge drawable will be set automatically, and everything toggle-related can be managed through a single object.
BadgeDrawerToggle
is a drop-in replacement forActionBarDrawerToggle
, and its constructors are exactly the same.