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>
Create Menu XML
Create menu folder in res folder. Create my_menu.xml file contains menus as below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu1"
android:title="@string/menu1"></item>
<item
android:id="@+id/menu2"
android:title="@string/menu2" />
<item android:title="@string/menu3" >
<menu >
<item
android:id="@+id/menu3_1"
android:title="@string/menu3_1" />
<item
android:id="@+id/menu3_2"
android:title="@string/menu3_2" />
</menu>
</item>
</menu>
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.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
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 inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu1) {
Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == R.id.menu2) {
Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == R.id.menu3_1) {
Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == R.id.menu3_2) {
Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
Structure of Project
Run App
Load Main Activity
Open Option Menu
Open Menu Item Have Children in Option Menu
Selected Menu in Option Menu