menu.xml :
< ?xml version="1.0" encoding="utf-8"?> < menu xmlns:android="http://schemas.android.com/apk/res/android"> < item android:id="@+id/item1" android:title="Option 1" android:icon="@android:drawable/ic_menu_compass"/> < item android:id="@+id/item2" android:title="Option 2" android:icon="@android:drawable/ic_menu_week"/> < item android:id="@+id/item3" android:title="Option 3" android:icon="@android:drawable/ic_menu_call"/> < item android:id="@+id/item4" android:title="Option 4" android:icon="@android:drawable/ic_menu_zoom"/> < /menu>Click on the menu button to open this menu in java class :
public class OptionMenuActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater=getMenuInflater(); inflater.inflate(R.layout.menu,menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if(item.getItemId()==R.id.item1) { Log.d("option","Option 1 was clicked"); } if(item.getItemId()==R.id.item2) { System.out.println("item2 selected"); } if(item.getItemId()==R.id.item3) { System.out.println("item3 selected"); } if(item.getItemId()==R.id.item4) { System.out.println("item4 selected"); } return super.onOptionsItemSelected(item); } }
Comments
Post a Comment