0% found this document useful (0 votes)
2 views

Android Menu

This document outlines the steps to design an Android application that displays a menu with sub-items. It includes instructions for creating a menu resource file, inflating the menu in the MainActivity, and adding functionality to show toast messages when menu items are clicked. The final steps involve running the application on an Android Virtual Device (AVD).

Uploaded by

a78134449
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Android Menu

This document outlines the steps to design an Android application that displays a menu with sub-items. It includes instructions for creating a menu resource file, inflating the menu in the MainActivity, and adding functionality to show toast messages when menu items are clicked. The final steps involve running the application on an Android Virtual Device (AVD).

Uploaded by

a78134449
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 10: Design an application for showing 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;

public class MainActivity extends AppCompatActivity {

@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 7: Run the application on AVD.

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>

Step 9: Run the application on AVD.

Step 10: To add toast message to be displayed on clicking on an item or sub-


item, update the MainActivity.java code.
Script to be added in the onCreate method:

@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);
}
}

Step 11: Run the application on AVD.

You might also like