Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...?

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

Vishesh Chandra


2 Answers

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.

like image 188
Srichand Yella Avatar answered Oct 04 '22 19:10

Srichand Yella


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.

like image 45
ccheneson Avatar answered Oct 04 '22 19:10

ccheneson