Android Menu
Android Menu
i. Add a Menu.
ii. Add sub-items in the menu.
iii. Show toast message on clicking of any item or
sub-item.
Step 1: Open a ‘New Project’ and name it ‘Menu’.
Step 2: Right click on ‘res directory’ and select New>Android Resource Directory.
A new window will open. Select the resource type as menu and click ‘OK’.
Step 3: A ‘menu’ will show in res>layouts.
Step 4: Right click on ‘menu’ and select New>Menu Resource File. A new window
will open. Keep the File Name as ‘option_menu’.
Step 5: Now under menu, option_menu.xml will be created. Open this XML file
and add items for the menu.
Script:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="Status"
android:id="@+id/status1"/>
<item
android:title="Settings"
android:id="@+id/settings1"/>
<item
android:title="Logout"
android:id="@+id/logout1"/>
</menu>
Step 6: In the MainActivity.java file add code for inflating the menu.
Script:
package com.example.menu;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflator =getMenuInflater();
inflator.inflate(R.menu.option_menu,menu);
return true;
}
}
Step 8: Now to add sub-items, add a menu inside the item which needs to have a
sub-item.
Script:
<?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">
<item
android:title="Status"
android:id="@+id/status1"/>
<item
android:title="Settings"
android:id="@+id/settings1"
app:showAsAction="never">
<menu>
<item
android:title="Account"
android:id="@+id/account"/>
<item
android:title="Chats"
android:id="@+id/chats"/>
<item
android:title="Notifications"
android:id="@+id/notifications"/>
<item
android:title="Storage and Data"
android:id="@+id/storage"/>
<item
android:title="Help"
android:id="@+id/help"/>
</menu>
</item>
<item
android:title="Logout"
android:id="@+id/logout1"/>
</menu>
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()) {
case R.id.status:
Toast.makeText(this, "Status Selected.",
Toast.LENGTH_SHORT).show();
case R.id.settings:
Toast.makeText(this, "Settings Selected.",
Toast.LENGTH_SHORT).show();
case R.id.account:
Toast.makeText(this, "Account Selected.",
Toast.LENGTH_SHORT).show();
case R.id.chats:
Toast.makeText(this, "Chats Selected.",
Toast.LENGTH_SHORT).show();
case R.id.notifications:
Toast.makeText(this, "Notifications Selected.",
Toast.LENGTH_SHORT).show();
case R.id.storage:
Toast.makeText(this, "Storage and Data Selected.",
Toast.LENGTH_SHORT).show();
case R.id.help:
Toast.makeText(this, "Help Selected.",
Toast.LENGTH_SHORT).show();
case R.id.logout:
Toast.makeText(this, "Logout Selected.",
Toast.LENGTH_SHORT).show();
default:
return super.onOptionsItemSelected(item);
}
}