Skip to content Skip to sidebar Skip to footer

Why Don't Inflated Views Respond To Click Listeners?

I'm trying to set a click listener for a button in my layout. The click listener is only triggered when I call findViewById() directly, and not when I take the view from the infla

Solution 1:

You only get the click event from the first method (the one that sends "Click" to LogCat) because you don't add anything that you inflate to your view hierarchy. The second line of your onCreate() method, setContentView(R.layout.test); takes care of inflating your views from the layout file AND adding them to the activity's view hierarchy. When you do the inflation manually a few lines later, you are forgetting to add rootLayout to the view hierarchy. Without doing this, there is nothing to click and hence no output on LogCat from your other onClick() method.

Solution 2:

Turns out I need to call setContentView( rootLayout ).

Post a Comment for "Why Don't Inflated Views Respond To Click Listeners?"