Search code examples
c#xamarin.androidandroid-7.0-nougatappcompatactivityandroid-7.1-nougat

OnOptionsItemSelected is not called in Xamarin.Android API 24+ (Android 7+)


I'm developing an app with Xamarin.Android that was working fine until the API 23 (Android 6.0):

This is my OnCreate event:

protected override void OnCreate(Bundle bundle)
{
    SetTheme(Resource.Style.MyCustomThemeCompat);
    base.OnCreate(bundle);

    base.SetActionBar(FindViewById<Toolbar>(Resource.Id.toolbarMain));
    ActionBar.Title = Title;

    ActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu_white_24dp);

    ActionBar.SetDisplayHomeAsUpEnabled(true);
}

And this my event OnOptionsItemSelected, which is never called since the API 24+ (Android 7+):

public override bool OnOptionsItemSelected(IMenuItem item)
{
    switch (item.ItemId)
    {
        case Android.Resource.Id.Home:
            if (MDrawerLayout.IsDrawerOpen(GravityCompat.Start))
            {
                MDrawerLayout.CloseDrawers();
            }
            else
            {
                MDrawerLayout.OpenDrawer(GravityCompat.Start);
            }

            return true;
        default:
            return false;
    }
}

My custom style:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
  <style name="MyCustomThemeCompat" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:colorPrimary">#01579B</item>
    <item name="android:colorPrimaryDark">#01579B</item>
    <item name="android:colorButtonNormal">#0277BD</item>
    <item name="android:textColorHint">#FAFAFA</item>
    <item name="android:textColor">#BDBDBD</item>
    <item name="android:textColorPrimary">#FFFFFF</item>
    <item name="android:windowBackground">#424242</item>
    <item name="android:elevation">4dp</item>
  </style>
</resources>

I haven't found any example or explanation of any change that I should do in my code. Most of examples use some Menus, but I don't need any. Also, I'm working with an AppCompatActivity and a Toolbar.

Right now, I just have a Hamburger menu in order to display the NavigationDrawer.

Toolbar:

Hamburger menu

Navigation Drawer opened:

Navigation Drawer

Out here is a small example of the full code:

https://github.com/FANMixco/NavigationDrawerError

Did anyone experience it before? Thanks for your advice.


Solution

  • First, modify your toolbarMain.xml from Android.Widget.Toolbar to Support.V7.Widget.Toolbar :

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbarMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?android:attr/actionBarSize"
        android:background="?android:attr/colorPrimary"/>
    

    Second, in your MainActivity :

    //base.SetActionBar(FindViewById<Toolbar>(Resource.Id.toolbarMain));
    //ActionBar.Title = "Típicos Salvadoreños";
            
    //ActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu_white_24dp);
    //ActionBar.SetDisplayHomeAsUpEnabled(true);
    
    MDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
    
    var mToolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbarMain);
    SetSupportActionBar(mToolbar);
    SupportActionBar.SetDisplayHomeAsUpEnabled(true);
    SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu_white_24dp);
    SupportActionBar.Title = "Típicos Salvadoreños";
    

    Then, OnOptionsItemSelected will be called when you click the Hamburger menu.

    Effect.

    Update :

    You could read the ActionBarDrawerToggle document, notice the two constructors there. The Toolbar type is android.support.v7.widget.Toolbar