OnCreateContextMenu () for EditText not working on real device

Just tried to test my application on a real device (HTC Desire Z with Android 2.2). And I found that my context menus do not work at all on EditText s. Otherwise, context menus work: in ListView , ImageView , etc. Everything works fine on the emulator ...

When I click EditText, it shows something like a zoom frame, and then it shows an unusual (not standard Android-like) context menu that says: "select text", "select all". It does not show my menus. Here are the screenshots:

but my menu, as in the emulator, see here

Here is the source code of my activity:

 public class MyActivity extends Activity { private static final String TAG=MyActivity.class.getName(); EditText editText; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText=(EditText )findViewById(R.id.editText); this.registerForContextMenu(editText); } @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) { Log.v(TAG, "Creating context menu for view="+view); menu.add(Menu.NONE, Menu.FIRST+1, Menu.NONE, "Test menu"); super.onCreateContextMenu(menu, view, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { Log.v(TAG, "Context item selected as="+item.toString()); return super.onContextItemSelected(item); } } 

I completely debugged / logged everything around my code, but Activity.onCreateContextMenu () didn’t even call (although it was registered accordingly).

Please help - what could it be? Is this related to HTC features?

+6
android android-edittext contextmenu android-compatibility
source share
2 answers

Yes, I think you see the HTC menu there.

The graphical editing context menu that you see is, I think, new to the later Desire models (Desire HD and Desire Z). What I see (with your code) in my original Desire is a pop-up text menu with a HeaderTitle heading set to "Edit Text" and a list, such as "Select All", "Copy", "Paste", etc. ., But I also see the entry "Test menu".

It occurs to me that when calling onCreateContextMenu () for things like ListView and ImageView, the menu object passed to this call is not populated by default by default. However, in the case of EditText, it is designed to interact with Clipboard, and therefore the system provides a pre-populated menu based on the state of the contents of the EditText (for example, if the text is selected, specify the Copy option; if the text in the clipboard contains the Paste option, etc. d.).

I managed to get the context menu without the "edit" parameters, clearing the title and contents, changing the code ...

 @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) { Log.v(TAG, "Creating context menu for view="+view); // Clear current contents menu.clearHeader(); menu.clear(); menu.setHeaderTitle("TEST MENU"); menu.add(Menu.NONE, Menu.FIRST+1, Menu.NONE, "Test menu"); super.onCreateContextMenu(menu, view, menuInfo); } 

The fact that you get a graphical menu (possibly obtained from ContextMenu) suggests that the above method will not work, so the only way to create it would be to create an instance of your own ContextMenu object, rather than using the one that was passed to onCreateContextMenu ().

I completely debugged / logged everything around my code, but still I didn’t even call Activity.onCreateContextMenu ()

This seems very strange - obviously, it is called for me, since I was able to play with ContextMenu, which is passed to it.

EDIT 1: Rethinking this, you say that you click "EditText" - is that what you are actually doing (short touch down and then finger up)?

To get my ContextMenu, I have to use a "long" click (press and hold for 1 second). When I just touch my EditText, a soft keyboard appears or, if the keyboard is already visible, the cursor simply moves to another place in the EditText field.

Obviously, Desire Z has a physical keyboard, and this can lead to slightly different behavior (as well as desire Z, which has a different version of the Sense interface for my desire).

The fact that onCreateContextMenu () is never called for you can mean only one thing, and exactly what you see is NOT ContextMenu and represents some type of pop-up user interface. At the very least, this is the only logical way to understand this.

Can you confirm that the “long” press still does not create ContextMenu for you or do you continue the long press?

If you tried a long press, try changing the code as follows:

  • Implement OnClickListener

     public class MyActivity extends Activity implements OnLongClickListener { 
  • Set the listener for editText in onCreate ...

     @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText=(EditText )findViewById(R.id.editText); this.registerForContextMenu(editText); editText.setOnLongClickListener(this); // <-- ADD THIS } 
  • Add listener code ...

     @Override public boolean onLongClick(View arg0) { android.util.Log.v(TAG, "onLongClick() called"); if (arg0 == editText) { android.util.Log.v(TAG, "arg0 == editText"); Toast.makeText(this, "onLongClick intercepted", 2000).show(); return true; } else { android.util.Log.v(TAG, "arg0 != editText"); return false; } } 

That way, I can intercept the long print and returning "true" from onLongClick (), I tell the system that I "absorbed" the event and it does not go through to cause the creation of my ContextMenu.

If this does not work for you, and a short press still causes a popup to appear, try implementing OnClickListener and override onClick ().

The object of the exercise is that if you can intercept everything that causes the pop-up to be created that you see, you can manually create and display your own ContextMenu.

+7
source share

This is the standard cut / copy / paste menu that you see.

See this topic for more details:

How to write your own context menu for copy and paste?

HJW Relations

-one
source share

All Articles