Timothy Steele
Timothy Steele

Reputation: 783

Android AppCompat Toolbar does not respond to touching action items

I'm following these instructions to implement an AppCompat Toolbar in my Android app. I am using AppCompat version 7:22.0.1 Also, I'm using this toolbar in addition to the actionBar (it's not replacing the ActionBar, as I see in many other examples).

The toolbar shows up, and it even shows the icon I specificed, but it does not respond when I touch the icon. Has anyone had a similar problem with the AppCompat Toolbar? Any suggestions on what to try next?

Here's my code from my browse() method in MainActivity.Java:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);

// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        // Handle the menu item
        switch (item.getItemId()) {
            case R.id.browse_back_button:
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
                restart(view);
            default:
                return false;
        }
    }
});

// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.toolbar_menu);

I placed this snippet of code in the XML layout file, as the first item in a RelativeLayout element.

<android.support.v7.widget.Toolbar
    android:id="@+id/my_awesome_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="#FFFF00" />

The Toolbar is inflated from a menu layout xml file. It consists of a menu element with a single 'item' element :

<item android:title="Back"
    android:id="@+id/browse_back_button"
    android:icon="@drawable/ic_action_back"
    app:showAsAction="always" />

Upvotes: 1

Views: 1184

Answers (1)

se_bastiaan
se_bastiaan

Reputation: 1704

The ListView is drawn on top of the Toolbar view and thus that is the view that will get the touch events. You should place the Toolbar as last element in the layout, or you should use a vertical LinearLayout, or you should use android:layout_below.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

  <ListView
      android:id="@+id/listViewBrowse"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="?attr/actionBarSize" />

  <android.support.v7.widget.Toolbar
      android:id="@+id/my_awesome_toolbar"
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:minHeight="?attr/actionBarSize"
      android:background="#FFFF00" />

</RelativeLayout>

 

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

  <android.support.v7.widget.Toolbar
      android:id="@+id/my_awesome_toolbar"
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:minHeight="?attr/actionBarSize"
      android:background="#FFFF00" />

  <ListView
      android:id="@+id/listViewBrowse"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      />

</LinearLayout>

 

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
       android:id="@+id/my_awesome_toolbar"
       android:layout_height="wrap_content"
       android:layout_width="match_parent"
       android:minHeight="?attr/actionBarSize"
       android:background="#FFFF00" />

    <ListView
        android:id="@+id/listViewBrowse"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:layout_below="@id/my_awesome_toolbar" />

</RelativeLayout>

 

I would use a LinearLayout here, since it is more convenient here. It does what you want to do. Align a view next to, not over, another view.

(EDIT: I see that this was commented already, don't get why. Because it is an answer to the question.)

Upvotes: 3

Related Questions