android toolbar navigation button id -


i have activity implements onclicklistener, , handling onclick event below code:

void onclick(view v){  final int id = v.getid();  switch(id){    case r.id.xxx:     break;  } } 

and have toolbar also, want handle toolbar navigation button click event in way too:

toolbar.setnavigationonclicklistener(this); 

but don't know id of toolbar navigation button. how can it?

if toolbar being used actionbar, view id android.r.id.home , use onoptionsitemselected(...) know when it's pressed.

if it's not being used actionbar, view id -1 doesn't have corresponding id resource defined.

which means must use setnavigationonclicklistener() in either of 2 approaches:

either:

    toolbar.setnavigationonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             ...         }     }); 

or

private view.onclicklistener homeclicklistener = new view.onclicklistener() {     @override     public void onclick(view view) {         ...     } };  @override protected void oncreate(...) {     ...     toolbar.setnavigationonclicklistener(homeclicklistener);     ... } 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -