Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting OnClickListener on the actionbar home icon makes the icon loose the press state

Setting the following makes the home icon show a press state:

actionBar.setHomeButtonEnabled(true);

But, after setting OnClickListener the home icon stops showing the press state:

ImageView iconImage = (ImageView) activity.findViewById(android.R.id.home);
iconImage.setOnClickListener(new android.view.View.OnClickListener() {
    @Override
    public void onClick(View v) {
    }
});

Any idea on how to prevent disabling the press state?

like image 598
AlikElzin-kilaka Avatar asked Feb 19 '23 08:02

AlikElzin-kilaka


1 Answers

To handle the click on the home icon , you do not need to set onClickListener, you need to do the following..

public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == android.R.id.home) { //app icon in action bar clicked; go back
        //do something
        return true;
    }

    return super.onOptionsItemSelected(item);
}
like image 139
Nermeen Avatar answered Feb 20 '23 21:02

Nermeen