0

Trying to add a menu to my toolbar, however the code supposedly required to do that is showing errors: enter image description here

Here's my other code:

res/menu/home_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/settings"
        android:title="@string/settings" />
    <item
        android:id="@+id/dark_theme"
        android:title="@string/dark_theme" />

</menu>

Here's the layout with the Toolbar I want to add the menu to:

res/layout/activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 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="?attr/actionBarSize"
        android:background="@color/colorVeryDark"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

</LinearLayout>

Any idea how I can add my menu to the toolbar?

Zorgan
  • 8,227
  • 23
  • 106
  • 207
  • http://stackoverflow.com/questions/27378981/how-to-use-searchview-in-toolbar-android/43485003#43485003 – Ali Apr 29 '17 at 22:55
  • Do you mean copy the menu.xml inside the Toolbar? I tried that and it's saying `element menu is not allowed here`. Isn't there a way to dynamically add it? – Zorgan Apr 29 '17 at 22:58
  • No, I mean you can add any view inside the toolbar. In my answer it's searchview but it can be for ex: a setting icon. You don't have to override OncreateOptionsMenu() – Ali Apr 29 '17 at 23:02
  • 2
    Move the `onCreateOptionsMenu()` method out of the `onCreate()` method. – Mike M. Apr 30 '17 at 01:42
  • Thanks @MikeM. it works now. – Zorgan Apr 30 '17 at 01:44

0 Answers0