Android, How to create a context menu ...

Here I wrote the code, but I don't get the output. Please tell me why this context menu is not displayed, where am I making a mistake ...? Please help me, thanks in Advance ....

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> 

MenuTest.java

 public class MenuTest extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater =getMenuInflater(); inflater.inflate(R.menu.more_tab_menu, menu); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo contextMenuInfo=(AdapterContextMenuInfo)item.getMenuInfo(); switch(item.getItemId()) { case R.id.feeds: break; case R.id.friends: break; case R.id.about: break; } return super.onContextItemSelected(item); } } 

Please tell me where I am making a mistake ...?

+4
source share
3 answers

Now you have this:

 super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater =getMenuInflater(); inflater.inflate(R.menu.more_tab_menu, menu); 

Change it like this:

 MenuInflater inflater =getMenuInflater(); inflater.inflate(R.menu.more_tab_menu, menu); return true; 

Also in onOptionsItemSelected:

 return true; 

Also use onCreateOptionsMenu and onOptionsItemSelected.

+5
source

You need to register your registerForContextMenu menu.

From this page

In order for a view to provide a context menu, you must "register" a context menu view. Call registerForContextMenu () and pass it the View you want to provide to the menu context. When this view then receives a long-press, it displays the menu context.

Your code above works fine. You just need to register a content menu to view it.

If you want to launch the context menu from anywhere on the screen:

Let's say your main.xml layout looks like this:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </LinearLayout> 

You will register the context menu that you created with the following (in onCreate ):

  LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout); registerForContextMenu(layout); 

So, if you run this in the emulator and make a long click on the Android desktop, your menu will appear.

+3
source

Replace this:

 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.more_tab_menu, menu); } 

Wherein:

 @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.more_tab_menu, menu); return true; } 

This will cause the menu items to appear when you press the Menu button on the phone.

0
source

All Articles