Vishesh Chandra
Vishesh Chandra

Reputation: 7071

Android, How to create Context Menu...

Here i wrote some code but not getting output.. Please tell me why is not displaying that context menu, where am i doing mistake...? Please guide 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 am i doing mistake...?

Upvotes: 6

Views: 26039

Answers (3)

Cameron Lowell Palmer
Cameron Lowell Palmer

Reputation: 22246

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);
}    

With this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

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

    return true;
}

This will result in the menu items being displayed when the Menu button on the phone is pressed.

Upvotes: 0

ccheneson
ccheneson

Reputation: 49410

You need to register your menu with registerForContextMenu.

From this page

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

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

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

Let's say your layout main.xml is like the following:

<?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 you have created with the following (in onCreate):

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

So if you run this in the emulator, and do a long-click on the Android desktop, your menu will pop up.

Upvotes: 3

Srichand Yella
Srichand Yella

Reputation: 4246

Right now you have this:

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

Change it to this:

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

Also in onOptionsItemSelected:

return true;

Also use onCreateOptionsMenu and onOptionsItemSelected.

Upvotes: 7

Related Questions