18

I am using XML file for creating Context Menu for my ListView. (Please see below). I also want to set a header for this Context Menu. I read (at http://www.mail-archive.com/android-developers@googlegroups.com/msg43062.html)that I can use menu.setHeaderTitle(myContextMenuTitle) in onCreateContextMenu Method. But I need to set this in XML file. How can I accomplish this?

Following is code for onCreateContextMenu Method, correct me if I am doing anything wrong.. This is my context_menu.xml file:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/open" android:title="Open"/>
</menu>

This is my onCreateContextMenu Method:

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

This is my onCreate Method:

@Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  //  extras = getIntent().getExtras();

  registerForContextMenu(getListView());

  ...
 }
Vasu
  • 4,862
  • 8
  • 42
  • 48

2 Answers2

18

You can call setHeaderTitle("mytitle") method in ,menu object . In override method you get menu object as paramrter of OnCreateContextMenu method. like this:

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
   super.onCreateContextMenu(menu, v, menuInfo);
   menu.setHeaderIcon(R.drawable.icon);
   menu.setHeaderTitle("Share Menu.");
   MenuInflater inflater = getMenuInflater();

   inflater.inflate(R.menu.contextmenu, menu);
 }     
sjngm
  • 12,423
  • 14
  • 84
  • 114
Ashish Saini
  • 2,328
  • 25
  • 21
10

You have to do it the way you are currently doing it.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119