How to add click's listener for app's icon on ActionBar in Android?

android, java

Solution

Add below code at onCreate() of Activity

mActionBar = getSupportActionBar(); 
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setHomeButtonEnabled(true); 

And here is listener

   @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {
            finish(); // Or do you own task 
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

Read more at Add Up Button for Low-level Activities. And Adding the Action Bar is a very good resource about ActionBar.

Problem

There is the following code: ``` getActionBar().setIcon(R.drawable.basket); ``` Now I want to set listener for clicking by this icon. Is it possible? Thanks in advance!

Original source