Reputation: 47
I am trying to show a menu. I can show the menu, but only with the ActionBar. But it's not going to toolbar. I tried using a Theme AppCompat without ActionBar, but the toolbar still empty.
menu_toolbar.xml - It's correct, it's working on ActionBar.
<?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"
xmlns:tools="http://schemas.android.com/tools"
>
<item
android:id="@+id/action_call"
android:title="Call"
app:showAsAction="always"
/>
</menu>
My manifest with the theme:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="OrderFoodApp.OrderFoodApp" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
>
</application>
</manifest>
My main layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize"
android:background="@color/toolbar"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
And my class (I tried with OnPrepareOptionsMenu too)
namespace OrderFoodApp
{
[Activity(Label = "OrderFoodApp", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
toolbar.Title = "Hi";
SetSupportActionBar(toolbar);
SetContentView(Resource.Layout.Main);
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.menu_toolbar, menu);
return base.OnCreateOptionsMenu(menu);
}
}
Upvotes: 0
Views: 70
Reputation: 4641
The Toolbar has to be on the same level as the DrawerLayout. You can make a relative layout to include the Toolbar at top and the DrawerLayout below.
Upvotes: 0
Reputation: 37414
You need to override OnCreateOptionsMenu
instead of OnPrepareOptionsMenu
because OnPrepareOptionsMenu
is for manipulating menu items only so use this
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.menu_toolbar, menu);
return true;
}
instead of this
public override bool OnPrepareOptionsMenu(IMenu menu){
MenuInflater.Inflate(Resource.Menu.menu_toolbar, menu);
return base.OnPrepareOptionsMenu(menu);
}
Upvotes: 1