0

I am setting my toolbar:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_back);
toolbar.setTitle(R.string.app_name);

How can I retrieve the home button (android.R.id.home) as a MenuItem?

Here's what I tried:

MenuItem back = menu.findItem(android.R.id.home);

But that makes a NullPointerException for some reason. So how can I retrieve it as MenuItem without getting a NullPointerException?

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117

2 Answers2

2

This SO answer, suggests the following code but it doesn't seem to work and I can't find anything else.

getWindow().getDecorView().findViewById(android.R.id.home);

In any case it is hacky, because you shouldn't need that reference, and you shouldn't be traversing the view hierarchy to get it in any case. Since the Android SDK purposefully hides this from you, it is not something you should attempt to get, as even if the above code did work, it may not work in a future version (which actually seems like a possible explanation for it not working for me as I tested in SDK 23 Marshmallow).

So, if you want to set the icon, you can do it by setting the android:homeAsUpIndicator in your theme, as suggested here.

If you want to detect a click event on the home button, the correct way is by overriding the onOptionsItemSelected method of Activity, for example

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case android.R.id.home:
            // do something
            return true;
    }
    return false;
}

Any other use would be outside the intended purpose and so not supported, as is agreed by others such as in this post.

Community
  • 1
  • 1
r3flss ExlUtr
  • 720
  • 7
  • 17
  • Since it's bad practice, is there an alternative? – Ali Bdeir Aug 17 '16 at 16:36
  • Using this code: `MenuItem back = (MenuItem) getWindow().getDecorView().findViewById(android.R.id.home); ` I still get `null` – Ali Bdeir Aug 17 '16 at 16:40
  • 1
    Well for a start, `findViewById` will return a `View`, and since `MenuItem` is not a subclass of `View` you cannot cast the object returned by `findViewById` to a `MenuItem`. But it doesn't work in any case. I assumed it would since the answer said it did and was accepted. I'll update the answer – r3flss ExlUtr Aug 17 '16 at 21:00
  • My use case for wanting to get a reference to that home button is so that I can tooltip/showcase/onboarding on it. I apply a different icon to that button (tapping it loads a filter menu). I want to do an onboarding tooltip kind of feature pointing at that button to tell users what that button is for. I want to reference that button so I can get its screen position/size to properly place my tooltip. – hellaandrew Feb 12 '22 at 15:34
-1

I didn't find out to get it as a MenuItem, but, to disable the home button pogrammatically:

getSupportActionBar().setDisplayHomeAsUpEnabled(true/false)

This is what I needed. But I still didn't find out how to get it as a MenuItem.

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117