How to Create Android Menus

Total Views : 95
Zoom In Zoom Out Read Later Print

This tutorial is all about create menu and perform some action on item selection.

In android menus are the one of the important part of the user interface. menu provide some action options. in this tutorial we are discussing about creating android menus.

1. Create a new project.
2. Create a folder menu in res folder if not exists, then create menu.xml in menu folder.
3. Open menu file and write the following code.

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- menu item -->
<item android:id="@+id/menu_search"
android:icon="@drawable/ic_search_black_24dp"
android:title="Search" />

<item android:id="@+id/menu_share"
android:icon="@drawable/ic_menu_share"
android:title="Share" />

<item android:id="@+id/menu_save"
android:icon="@drawable/call_favorite"
android:title="Save" />

<item android:id="@+id/menu_delete"
android:icon="@drawable/ic_action_delete"
android:title="Delete" />

<item android:id="@+id/menu_settings"
android:icon="@drawable/setting_icon"
android:title="Settings" />
</menu>

4. For this example we don't need any code for xml activity so leave it as default or add something if you want.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

5. Open your MainActivity and write following code.

MainActivity.java

package com.prodevsblog.androidmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

// Initiating Menu XML file (menu.xml)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return handleClick(item);
}
boolean handleClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_search:
Toast.makeText(MainActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(MainActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;

case R.id.menu_share:
Toast.makeText(MainActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;

case R.id.menu_delete:
Toast.makeText(MainActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;

case R.id.menu_settings:
Toast.makeText(MainActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

In above we code firs we are inflating menu.xml then in item click we are handling each item click using switch case.

See More

Latest Photos