Search code examples
c#androidvisual-studioxamarinandroid-toolbar

Toolbar in Xamarin Android not showing icons


I'm trying to create a toolbar to save items in my application. But my toolbar is not showing properly.

It is showing like this I want to display like this

In my Fragment

    public override void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
    {
        inflater.Inflate(Resource.Menu.menu_RefuelingToolbar, menu);
    }

menu_RefuelingToolbar.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          tools:context=".RefuelingFragment">
          <item android:id="@+id/button_Save"
                android:icon="@drawable/ic_done_white_24dp"
                android:showAsAction="always"
                android:title="Settings" />
    </menu>

Solution

  • If you extend AppCompatActivity instead of Activity like this :

    public class MainActivity : AppCompatActivity {...}//instead of Activity
    

    In this case, the toolbar cant showing picture, the reason is that :

    When using the appcompat library, menu resources should refer to the showAsAction in the app: namespace, not the android: namespace. Similarly, when not using the appcompat library, you should be using the android:showAsAction attribute.

    I think the problem is that you are mixing Framework Activity and AppCompat menu.

    You should use AppCompatActivity with AppCompat Action bar and app:showAsAction; or Activity with android:showAsAction.

    EDIT : Modify your code in menu_MGradeToolbar.xml like this :

    <?xml version="1.0" encoding="utf-8" ?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       >
    <item android:id="@+id/button_MGradeSave"
        android:icon="@drawable/ic_done_white_24dp"
        app:showAsAction="ifRoom"
        android:title="Save" />
    </menu>
    

    Here is the result.