In android menu is use fro any task like search,delete etc. Here we are using option menu. In this menu will be open when we click on menu. a list will be open that show the items we entered in the menu.
In activity_main.xml file we take LinearLayout and a text box.
In menu.xml file we take three options next, previous and list options to select.
In java file we give all the menu values in the toast. Means the value selected by user will display on the screen for short period of time.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textSize="20sp" android:textStyle="bold" android:capitalize="none" android:typeface="sans"/>
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.view.MenuInflater;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.next:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.next) + " menu option",
Toast.LENGTH_SHORT).show();
return true;
case R.id.previous:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.previous) + " menu option",
Toast.LENGTH_SHORT).show();
return true;
case R.id.list:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.list) + " menu option",
Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/next"
android:icon="@drawable/ic_next"
android:title="@string/next" />
<item android:id="@+id/previous"
android:icon="@drawable/ic_previous"
android:title="@string/previous" />
<item android:id="@+id/list"
android:icon="@drawable/ic_list"
android:title="@string/list" />
</menu>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">Click on the Options Menu to view the available Menu Options</string>
<string name="app_name">Options Menu Example</string>
<string name="next">Next</string>
<string name="previous">Previous</string>
<string name="list">Show List</string>
</resources>
In activity_main.xml file we take LinearLayout and a text box.
In menu.xml file we take three options next, previous and list options to select.
In java file we give all the menu values in the toast. Means the value selected by user will display on the screen for short period of time.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textSize="20sp" android:textStyle="bold" android:capitalize="none" android:typeface="sans"/>
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.view.MenuInflater;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.next:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.next) + " menu option",
Toast.LENGTH_SHORT).show();
return true;
case R.id.previous:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.previous) + " menu option",
Toast.LENGTH_SHORT).show();
return true;
case R.id.list:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.list) + " menu option",
Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/next"
android:icon="@drawable/ic_next"
android:title="@string/next" />
<item android:id="@+id/previous"
android:icon="@drawable/ic_previous"
android:title="@string/previous" />
<item android:id="@+id/list"
android:icon="@drawable/ic_list"
android:title="@string/list" />
</menu>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">Click on the Options Menu to view the available Menu Options</string>
<string name="app_name">Options Menu Example</string>
<string name="next">Next</string>
<string name="previous">Previous</string>
<string name="list">Show List</string>
</resources>