The Wayback Machine - https://web.archive.org/web/20240812083118/https://www.geeksforgeeks.org/how-to-create-menu-folder-menu-file-in-android-studio/
Open In App

How to Create Menu Folder & Menu File in Android Studio?

Last Updated : 07 Mar, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

  1. Options menu and app bar
  2. Context menu and contextual action mode
  3. 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.

Import External JAR Files in Android Studio

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"?>
      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 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 store the animation file under the resource folder
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, software written in the Java/Kotlin programming lang
2 min read
How to fix "Android Studio doesn't see device" in Android Studio?
In Android Studio, sometimes the list of devices and emulators doesn't list your physical device when you try to plug it in. Or you may have faced a situation when you plugged the phone in for the first time, no dialog appeared asking if you trust the computer. But the laptop is already allowed to connect to the mobile device. However, the button R
4 min read
How to Fix "Android studio logcat nothing to show" in Android Studio?
Logcat Window is the place where various messages can be printed when an application runs. Suppose, you are running your application and the program crashes, unfortunately. Then, Logcat Window is going to help you to debug the output by collecting and viewing all the messages that your emulator throws. So, this is a very useful component for the ap
2 min read
Assets Folder in Android Studio
It can be noticed that unlike Eclipse ADT (App Development Tools), Android Studio doesn’t contain an Assets folder in which we usually use to keep the web files like HTML. Assets provide a way to add arbitrary files like text, XML, HTML, fonts, music, and video in the application. If one tries to add these files as "resources", Android will treat t
4 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 drawable folder contains the different types of i
3 min read
Resource Raw Folder in Android Studio
The raw (res/raw) folder is one of the most important folders and it plays a very important role during the development of android projects in android studio. The raw folder in Android is used to keep mp3, mp4, sfb files, etc. The raw folder is created inside the res folder: main/res/raw. So we will simply create it inside the res folder. But befor
4 min read
How to Add Resource File in Existing Android Project in Android Studio?
Android resource file is a file that stores resources needed for the UI part of an android application. Like: Layout files, drawable files, etc. In the android application, it is very often that we need another resource file for another purpose in the existing application. There are many types of resource files Layout files, Drawables, Menus are so
1 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 several different types of drawable resources files a
3 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 difference between a Jar and an AAR is that AAR s inclu
3 min read
Article Tags :
three90RightbarBannerImg