How to Create Menu Folder & Menu File in Android Studio?
Last Updated :
07 Mar, 2021
Menus are the common user interface component in almost all the application. This interface provides a few options from which the user can select an option. There are 3 types of menu types available in Android:
- Options menu and app bar
- Context menu and contextual action mode
- Popup menu
How to define Menu in XML?
In android, we can build the menu on our activity page but which is not standard practice, instead we define it in the menu resource then we inflate that menu resource into our activity. The reason why we should define menu in menu resource:
- The first and most important thing it separates from our activity so visualization and debugging is easy.
- Separating the menu gives us flexibility like for different platform versions, screen sizes we can make some changes to our menu resource file.
- Easy to manage as it’s separate from our activity.
Step by Step Implementation
Step 1: Open your project in "Project" mode
Open your android project in “Project” mode If the project is already opened in the “Android” mode.
Step 2: In your project Go to the app > src > main > res as shown in the below image.
Step 3: Right-click on the res folder > New > Android Resource Directory as shown in the below image.
Step 4: Now you select the menu option from the values drop-down section.
After selecting you can see a screen like this.
And now just hit OK.
Step 4: Now you can see the menu folder inside the res folder.
Step 5: Now to create a menu file select the menu folder and right-click on it and select Menu Resource File.
Step 6: Give a name to your menu file, for instance here we have given the name as my_menu.
Then hit OK. Now you can see the my_menu.xml file inside the menu folder. Woah, you just create your menu file in android studio. So now we just write few lines of code to see that our menu file is working or not.
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/new_game"
android:icon="@drawable/ic_baseline_local_see_24"
android:title="@string/new_game"/>
<item android:id="@+id/help"
android:icon="@drawable/ic_baseline_first_page_24"
android:title="@string/help" />
</menu>
Now look at the codes you see the menu tag i.e <menu>
- <menu>: It is just a container and it holds the menu items or group of menu items.
- <item>: It creates menu item
Title and icon properties are self-explanatory so I don’t think I have to explain them.
Note: Look at the above code, android: icon, those are the icon that we have created. In your case just look at your drawable folder and give your icon name there.
Ok, we just completed our 80% of work now just inflate our menu to our MainActvity.kt file
Kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
// look at this section
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.my_menu,menu)
return super.onCreateOptionsMenu(menu)
}
}
Just focus on the codes after the comment line that block of codes used to inflate our menu in our app. Now run your app and you can see the result.
Output:

Similar Reads
How to Create a New Fragment in Android Studio? Android Studio is the official integrated development environment for Google's Android operating system, built on JetBrains' IntelliJ IDEA software and designed specifically for Android development. You can Develop Android App using this. Here, We are going to learn how to create a new Fragment in A
2 min read
How to Add Image to Drawable Folder in Android Studio? The resource folder is the most important folder because it contains all the non-code sources like images, XML layouts, UI strings for the android application. In Android Studio inside the res folder, one can find the drawable folder, layout folder, mipmap folder, values folder, etc. Among them, the
3 min read
How to Create Anim Folder & Animation File in Android Studio? The animation is a method in which a collection of images is combined in a specific way and processed then they appear as moving images. Building animations make on-screen objects seem to be alive. To apply Animations to the Application sometimes we need to make an anim folder in Android Studio to s
2 min read
How to Create New Package Inside Src Folder in Android Studio? A package is a namespace that combines a set of relevant classes and interfaces. Conceptually one can consider packages as being similar to various folders on your computer. One might have HTML pages in one folder, images in another, and scripts or applications in yet another. Because in android, so
2 min read
How to Create Google Glass Options Menu in Android? Google Glass is a wearable device developed by Google that allows users to perform various tasks such as taking photos, recording videos, and sending messages. In this article, we will show you how to create a Google Glass options menu in Android. Step By Step Implementation Step 1: To create a new
2 min read
How to Create Drawable Resource XML File in Android Studio? Prerequisite: Android Project folder Structure A drawable resource is a common concept for a graphic that can be drawn to the screen and which one can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon. There are sev
3 min read
How to Create/Start a New Project in Android Studio? After successfully installing the Android Studio and opening it for the first time. We need to start with some new projects to start our journey in Android.In this article, we will learn about How to Create a New Project in Android Studio.Steps to Create/Start a New Android Project in Android Studio
2 min read
Different Ways to Create aar File in Android Studio Android Studio can be used to create an Android archive file (*.aar) that can contain classes and methods that make use of Android classes and related files. Like creating the Jar file, an Android project must be created first, then the Android library module can be created and added. The main diffe
3 min read
How to Get Image from Image Asset in Android Studio? While Developing an App, we require many images as an icon in our app. Here we are going to explain how we can get image in Android Studio using Image Asset. Creating an attractive launcher icon for your app, that the user will first come across while looking at your app, anyway needs more brainstor
1 min read
How to Create Landscape Layout in Android Studio? In Android, whenever the user switches to landscape mode an issue is encountered in which some of the widgets become invisible (as you can see in the below image) and so in this scenario, there is a need to design a separate layout for the landscape mode. So in android, every application is designed
3 min read