Klette
Klette

Reputation: 286

onTextContextMenuItem is not called on custom menu items

I'm trying to add some custom menu items to some EditText-instances. They appear on in the menu, but when I click the buttons the onTextContentMenuItem-method is not called. The EditText-instances are in a ListView if that matters.

Any advice on the matter?

Relevant code:

class DocumentFragment extends EditText {

  public DocumentFragment(Context context) {
   super(context);
  }

  @Override
  public void onCreateContextMenu(ContextMenu menu) {
   menu.add(Menu.CATEGORY_ALTERNATIVE, CONVERT_TO_H1, Menu.NONE, "Convert to H1");
      menu.add(Menu.CATEGORY_ALTERNATIVE, CONVERT_TO_P, Menu.NONE,  "Convert to P");
      super.onCreateContextMenu(menu);
  }

  @Override
  public boolean onTextContextMenuItem(int id) {
   Log.i("ID", String.valueOf(id)); // The id of CONVERT_TO_{H1,P} never appears.
   return super.onTextContextMenuItem(id);
  }
 }

Upvotes: 1

Views: 2146

Answers (2)

Marcos
Marcos

Reputation: 11

It's very easy... after look for in the EditText and TextView sources...

First, you must implement the interface MenuItem.OnMenuItemClickListener in your EditText class.

Second, you must implement the onMenuItemClick for the interface, like so

@Override
public boolean onMenuItemClick(MenuItem item)
{
    return onTextContextMenuItem( item.getItemId() );
}

Last, add to each menu item the listener,

menu.add( Menu.NONE, MyMenu, Menu.CATEGORY_SECONDARY, "Menu text" ) ).setOnMenuItemClickListener( this );

Upvotes: 1

Łukasz Wiśniewski
Łukasz Wiśniewski

Reputation: 487

This will be called in Activity.onContextItemSelected(MenuItem item). You have to handle it from there.

Upvotes: 0

Related Questions