In android, we oftentimes need to create Option Menu, Here are two methods we need to override as shown in example given below-
public class DialogActivity extends Activity {
private static final int ADD_LOC = Menu.FIRST;
private static final int CHANGE_PASS = Menu.FIRST+1;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate (savedInstanceState);
setContentView(R.layout.main);
}
/*====================== Option menu creation==========================*/
/*------------------------------------menu creation--------------------------------------------*/
@Override
public boolean onCreateOptionsMenu(Menu menu){
menu.add(0,ADD_LOC,0,"Add Location").setIcon(R.drawable.add);
/* 1st argument->group id, ADD_LOC -> item_id, 3rd argument -> appearance of item in menu,
last argument -> Name which will apper on the menu */
menu.add(0,CHANGE_PASS,0,"Rename").setIcon(R.drawable.change);
//AlphabeticShortcut(alphaChar) can be used here for selection item through pressing a key Input
return true;
}
/*-----------------------------------Item Selection-----------------------------------------------*/
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case ADD_LOC:
Toast.makeText(DialogActivity.this, "So, YOu can EDIT ?", Toast.LENGTH_LONG).show();
// Your code goes here for case One..., Here I have given a Toast on option item selection
return true;
case CHANGE_PASS:
Toast.makeText(DialogActivity.this, "So, YOu can RENAME ?", Toast.LENGTH_LONG).show();
// Your code goes here for case Two...
return true;
}
return false;
}
/*------------------------------------------------------------------------------------------------------*/
/*====================================================================*/
}
No comments:
Post a Comment