5

I'm using AppCompat and trying to recall the ImageView for the up/back button belonging to the toolbar.

I know R.android.id.home exists, because I can manage its click as a Menu item:

public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
         //this works
    }
    return super.onOptionsItemSelected(item);
}

Apart from that, whenever I try to call findViewById(android.R.id.home) - be it onCreate, be it onClick of a custom button - I get null. I even get null if, in the sample above, I call findViewById(item.getItemId()).

Why is it? This question has been asked before here, most times regarding ActionBarSherlock (which I am not using). Another time it was suggested to use:

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

But it isn't working. In that question the OP also says findViewById(android.R.id.home) works on API>3.0, but that's not true for me. Any ideas?

Community
  • 1
  • 1
natario
  • 24,954
  • 17
  • 88
  • 158
  • 1
    "I know R.android.id.home exists, because I can manage its click as a Menu item" -- that is a menu ID. It is not necessarily a widget ID. – CommonsWare Dec 31 '14 at 15:48
  • @Commons so there's no way to find the related view? I'm was hoping so because of the question linked. – natario Dec 31 '14 at 15:51
  • 1
    I have no idea. Any solution you find will be intrinsically unreliable, as *it is not your widget*. It is the action bar implementation's widget. The action bar can name it whatever it wants, use whatever `View` class it wants (or even directly render it to the `Canvas`), etc. Do not assume, or attempt to control, the internal implementation of somebody else's UI. – CommonsWare Dec 31 '14 at 15:53
  • @Commons OK I give up. I'll accept this answer if you post it. [ Strongly related: maybe you can help me [here](http://stackoverflow.com/questions/27680915/customizing-up-back-burger-background-in-my-actionbar). The only answer doesn't seem OK to me - do one really has to make his own Up button to style it?] – natario Dec 31 '14 at 16:03

2 Answers2

3

Whether or not the "home" icon is a widget, and what class of widget it is, and what its ID is (if any), is up to the implementation of the action bar. The native action bar may do this differently for different API levels, and all of that may be different than the way appcompat-v7 does it. Let alone ActionBarSherlock or other action bar implementations.

Specifically, android.R.id.home is a menu ID, which is why you can use it in places like onOptionsItemSelected(). It is not necessarily a widget ID, which is why it may or may not work with findViewById().

Ideally, you do not attempt to mess with the internal implementation of a UI that you did not construct yourself.

do one really has to make his own Up button to style it?

I do not know, as I have never tried to style it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

As CommonsWare said android.R.id.home is a menu ID, not a widget ID. But if you want to access this home button you could do it. For example I needed it to highlight home button in in-app tutorial:

fun AppCompatActivity.getToolbarHomeIcon(): View? =
    this.findViewById<Toolbar?>(R.id.toolbar)?.let { toolbar ->
        val contentDescription: CharSequence = toolbar.navigationContentDescription.let {
            if (it.isNullOrEmpty()) {
                this.getString(R.string.abc_action_bar_up_description)
            } else {
                it
            }
        }
        // Here home button should be created even if it doesn't exist before
        toolbar.navigationContentDescription = contentDescription

        ArrayList<View>().let { potentialViews ->
            toolbar.findViewsWithText(
                potentialViews,
                contentDescription,
                View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION
            )

            potentialViews.getOrNull(0)
        }
    }
Oleksandr Albul
  • 1,611
  • 1
  • 23
  • 31