0

Can I use the include statement to create a menu.xml file in another activity? I have tried this line of code in the second activity xml file. It crashes the app. Is my syntax correct?

<include layout="@menu/menu_main"/>

So every time the menu is needed it has to be inflated?

James_Duh
  • 1,321
  • 11
  • 31

2 Answers2

1

I have tried this with various include syntax statements according to this post you can not use the include statement so YES you will need to inflate the menu each time it is need. HERE

Community
  • 1
  • 1
Vector
  • 3,066
  • 5
  • 27
  • 54
0

Yes, you can do this. For example, this is what I do when I need to replicate a navigation drawer in different activities:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


    </FrameLayout>

    <!-- The navigation drawer -->

    <include layout="@layout/nav_drawer"/>


</android.support.v4.widget.DrawerLayout>
ig343
  • 277
  • 1
  • 3
  • 17
  • Your nav_drawer is in a layout folder and James_Duh has a menu.xml file that is in a menu folder so look at the first two words in the statement include layout so my novice guess is that android goes looking for the menu in the layout folder and when it is not found CRASH – Vector Oct 13 '16 at 17:42
  • @iguarna I did post my include statement that is used in the activity above – James_Duh Oct 13 '16 at 17:48
  • The remark made by @Grendel is correct. You should store it in the layout folder – ig343 Oct 13 '16 at 21:32