mercredi 13 mai 2015

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" />

Aucun commentaire:

Enregistrer un commentaire