Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does findViewById(R.android.id.home) always return null?

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?

like image 382
natario Avatar asked Sep 29 '22 02:09

natario


2 Answers

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.

like image 83
CommonsWare Avatar answered Oct 06 '22 01:10

CommonsWare


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)
        }
    }
like image 44
Oleksandr Albul Avatar answered Oct 06 '22 03:10

Oleksandr Albul