简体   繁体   中英

How to get OnClick on the icon/logo of an ActionBar

How can I get on click action, when I clicked actionbar logo. I tried

onOptionsItemSelected

case android.R.id.home://here to get action

and it does not work. I tried to add onClick listener on my decor view like this at onCreate

getWindow().getDecorView().setOnClickListener(this);
getWindow().getDecorView().setOnTouchListener(this);
getWindow().getDecorView().getRootView().setOnClickListener(this);
getWindow().getDecorView().getRootView().setOnTouchListener(this);

and I implemented simple listener

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "onTouch pressed", Toast.LENGTH_LONG).show();

    return false;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "onClick pressed", Toast.LENGTH_LONG).show();
}

But it does not enter none of the methods. How can I get OnClick action on icon or onclick action when user clicked at Top Left corner of my decorview

First set the logo to be a clickable button:

myActionBar.setHomeButtonEnabled(true);

Then, override this method to get clicks:

public boolean onMenuItemSelected(int featureId, MenuItem item)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM