06 Aug, 2018
Categories: Android
Create Project
Create new project in Android Studio with steps as below:
Step 1: Input Project Name and Select Project Location
Step 2: Select SDK for Android App
Step 3: Select Default Activity for App
Step 4: Finish create project
Add Strings
Open res\values\strings.xml file and add new string as below:
<resources>
<string name="app_name">Learn Android with Real Apps</string>
<string name="menu1">Menu 1</string>
<string name="menu2">Menu 2</string>
<string name="menu3">Menu 3</string>
<string name="menu3_1">Menu 3.1</string>
<string name="menu3_2">Menu 3.2</string>
</resources>
Main Activity Layout
Open res\layout\activity_main.xml file and create layout as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</LinearLayout>
Main Activity Class
Add code to MainActivity.java in android.demo.learnandroidwithrealapps package as below:
package android.demo.learnandroidwithrealapps;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int MENU1 = 0;
private static final int MENU2 = 1;
private static final int MENU3 = 2;
private static final int MENU_1_ITEM = Menu.FIRST;
private static final int MENU_2_ITEM = Menu.FIRST + 1;
private static final int MENU_3_ITEM = Menu.FIRST + 2;
private static final int MENU_3_1_ITEM = MENU_3_ITEM + 1;
private static final int MENU_3_2_ITEM = MENU_3_ITEM + 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(MENU1, MENU_1_ITEM, 0, getText(R.string.menu1));
menu.add(MENU2, MENU_2_ITEM, 1, getText(R.string.menu2));
SubMenu menu3 = menu.addSubMenu(MENU3, MENU_3_ITEM, 2, getText(R.string.menu3));
menu3.add(MENU3, MENU_3_1_ITEM, 0, getText(R.string.menu3_1));
menu3.add(MENU3, MENU_3_2_ITEM, 1, getText(R.string.menu3_2));
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == MENU_1_ITEM) {
Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == MENU_2_ITEM) {
Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == MENU_3_1_ITEM) {
Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == MENU_3_2_ITEM) {
Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
Structure of Project
Run App
Load Main Activity
Option Option Menu
Open Menu Item Have Children in Option Menu
Selected Menu in Option Menu