I use ShowcaseView library for app tutorial. I need to get a reference to Navigation Drawer toggle button(aka "burger button"):
I use Toolbar as Actionbar, and I have no idea how to get this button. Usually to toggle drawer I use this:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
toggleDrawer(Gravity.START);
}
}
But when I use Device Monitor to make snapshot of the screen, there's no view with id "home".
Any suggestions?
In the original
ActionBar
, the twoView
s commonly shown on the left are the UpView
and the HomeView
. The UpView
is the button that usually displays the "hamburger" icon, while the HomeView
displays the app icon. Things have changed with theToolbar
class. There is no longer a defaultView
with theandroid.R.id.home
ID, though setting aToolbar
as the supportActionBar
will still fire theonOptionsItemSelected()
with the appropriate item ID when theView
in question is clicked.The current
Toolbar
class now refers to the UpView
internally as the Nav ButtonView
, and it creates thatView
dynamically. I've used two different methods to get a reference to that - one that iterates over aToolbar
's childView
s, and one that uses reflection on theToolbar
class.For the iterative method, after you've set the toggle, but before you've added any other
ImageButton
s to theToolbar
, the Nav ButtonView
is the onlyImageButton
child of theToolbar
, which you can get like so:If you need a reference to the app icon's
View
- that which corresponds to the old HomeView
- you can use a similar method looking for anImageView
.The reflective method, on the other hand, can be used at any time after the
View
has been set on theToolbar
.Again, a similar method can be used for the icon
View
, retrieving instead the"mLogoView"
Field
.In your Xml file just add a view in leftmost of toolbar/Layout, and use this view in your showcase target