Creating a Menu Resource
you should define a menu and all its items in an XML menu resource, then inflate the menu resource (load it as a programmable object) in your application code.To create a menu resource, create an XML file inside your project's res/menu/ directory.
example.
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item1" android:icon="@android:drawable/ic_menu_edit" android:title="btn1"> </item> <item android:id="@+id/item2" android:icon="@android:drawable/ic_menu_add" android:title="btn2"> </item> </menu>
Creating an Options Menu
When Press the menu key, Android Framework is called onCreateOptionsMenu method in Activity class.To inflate a menu resource.
@Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.menu,menu); return true; }
Responding to user action
When User selected in menu item, Android Framework is called onOptionsItemSelected method in Activity class.@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.item1: //action item1 return true; case R.id.item2: //action item2 return true; default: return super.onOptionsItemSelected(item); } }
No comments:
Post a Comment