Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, How to create option Menu

Here I tried to make option menu, but menu is not displaying on screen, so please guide me where am I doing mistake...

MenuTest.java

public class MenuTest extends Activity {    @Override public boolean onCreateOptionsMenu(Menu menu) {     MenuInflater inflater=getMenuInflater();     inflater.inflate(R.menu.more_tab_menu, menu);     return super.onCreateOptionsMenu(menu);  } @Override public boolean onOptionsItemSelected(MenuItem item) {     switch(item.getItemId())     {     case R.id.feeds:         break;     case R.id.friends:         break;     case R.id.about:         break;     }     return true; } } 

And my XML file is more_tab_menu.xml

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item     android:id="@+id/feeds"     android:title="Feeds"/> <item     android:id="@+id/friends"     android:title="Friends"/> <item     android:id="@+id/about"     android:title="About"/> </menu> 

Please guide me,

like image 656
Vishesh Chandra Avatar asked Jun 22 '11 11:06

Vishesh Chandra


People also ask

What is android option menu?

The options menu is the primary collection of menu items for an activity. It's where you should place actions that have a global impact on the app, such as "Search," "Compose email," and "Settings." See the section about Creating an Options Menu.


2 Answers

public class MenuTest extends Activity {      @Override     public boolean onCreateOptionsMenu(Menu menu) {         MenuInflater inflater = getMenuInflater();         inflater.inflate(R.menu.more_tab_menu, menu);          // return true so that the menu pop up is opened         return true;      } } 

and don't forget to press the menu button or icon on Emulator or device

like image 129
DEVANG SHARMA Avatar answered Oct 11 '22 18:10

DEVANG SHARMA


please see :==

private int group1Id = 1;  int homeId = Menu.FIRST; int profileId = Menu.FIRST +1; int searchId = Menu.FIRST +2; int dealsId = Menu.FIRST +3; int helpId = Menu.FIRST +4; int contactusId = Menu.FIRST +5;  @Override public boolean onCreateOptionsMenu(Menu menu) {     menu.add(group1Id, homeId, homeId, "").setIcon(R.drawable.home_menu);     menu.add(group1Id, profileId, profileId, "").setIcon(R.drawable.profile_menu);     menu.add(group1Id, searchId, searchId, "").setIcon(R.drawable.search_menu);     menu.add(group1Id, dealsId, dealsId, "").setIcon(R.drawable.deals_menu);     menu.add(group1Id, helpId, helpId, "").setIcon(R.drawable.help_menu);     menu.add(group1Id, contactusId, contactusId, "").setIcon(R.drawable.contactus_menu);      return super.onCreateOptionsMenu(menu);  }  @Override public boolean onOptionsItemSelected(MenuItem item) {     switch (item.getItemId()) {     case 1:         // write your code here         Toast msg = Toast.makeText(MainHomeScreen.this, "Menu 1", Toast.LENGTH_LONG);         msg.show();         return true;      case 2:         // write your code here         return true;      case 3:         // write your code here         return true;      case 4:         // write your code here         return true;      case 5:         // write your code here         return true;      case 6:         // write your code here         return true;      default:         return super.onOptionsItemSelected(item);     } } 
like image 29
Patel Ekta Avatar answered Oct 11 '22 19:10

Patel Ekta