Saturday 2 November 2013

Menu Example

Menus are a common user interface component in many types of applications. To provide a familiar and consistent user experience, you should use the Menu APIs to present user actions and other options in your activities.

Beginning with Android 3.0 (API level 11), Android-powered devices are no longer required to provide a dedicated Menu button. With this change, Android apps should migrate away from a dependence on the traditional 6-item menu panel and instead provide an action bar to present common user actions.

Although the design and user experience for some menu items have changed, the semantics to define a set of actions and options is still based on the Menu APIs. Here i will shows you how to how to create the three fundamental types of menus or action presentations on all versions of Android.

Type of Menus 


  1. Options Menu
  2. Context Menu
  3. Popup Menu


Options Menu:

Steps to create options menu:

First of all create a project by the name of MenuExample



Now open main.xml file from menu folder 



Now click on Add button


Select Item from popup window


Fill Title & Icon field


Now press Ctrl+s to save menu item, follow the same process for another menu item



Now we need to override "onOptionsItemSelected()" to define functionality of menu in MainActivity.java


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case R.id.item1:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;

case R.id.item2:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;

case R.id.item3:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item4:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item5:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item6:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item7:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;
}

return super.onOptionsItemSelected(item);





That's it ............ :)

I show you Context and Popup menu example in next post

Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

No comments:

Post a Comment