4

My EditText is not displaying the default ContextMenu (copy, paste, select, selectall) after a long press. Do I have to create my own ContextMenu?

Below is snippets from a function that is called to create a popup menu where this EditText resides.

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    popupView = layoutInflater.inflate(R.layout.add_recipe_pop,null);
    final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    EditText recipe_url = (EditText)popupView.findViewById(R.id.recipe_url_text);
    recipe_url.setLongClickable(true);
    registerForContextMenu(recipe_url);
    popupWindow.setFocusable(true);
    popupWindow.update();
    popupWindow.showAtLocation(v,Gravity.CENTER,0,0);

This is part of the add_recipe_pop XML and the EditText is just in a

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#E6E6E6"
    android:orientation="vertical" >

<EditText
     android:id="@+id/recipe_url_text"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:hint="@string/add_recipe_hint"
     android:inputType="textUri"
   />

I have tried toying with the EditText focusable and setTextSelectable attribute but the keyboard doesn't appear if I do. Thank you for your help! :)

Justin Tan
  • 43
  • 3
  • that fecility is called clipboard, check this link. http://stackoverflow.com/questions/25580288/clipboard-manager-activity-not-working-in-android – balaji koduri Dec 27 '14 at 07:41
  • use setfocusableintouchmode.. and no you do not have to create your own context menu, is device default.. and remove the `recipe_url.setLongClickable(true); registerForContextMenu(recipe_url);` – Elltz Dec 27 '14 at 08:18
  • @Elltz I tried using `recipe_url.setFocusableInTouchMode(true);`but the context menu still doesn't show up :/ – Justin Tan Dec 27 '14 at 18:23
  • @balajikoduri I tried doing that and it worked but it will only allow me to paste or copy but I want the ability to do all the functions (copy, paste, select, selectall) – Justin Tan Dec 27 '14 at 18:44

3 Answers3

2

This is old but for anyone looking at this I have found there is no way around this.

For some reason any edit text inside a popup will work but will not work with the copy/paste menu.

In fact you can set any text selectable and it won't work.

The easiest way around this is simply to not use a popup menu. If you change your code to set the content view to the popup menu layout it will work.

Why it doesn't work in the popup menu I don't know, but not using the menu is what works.

Jeremy Styers
  • 497
  • 5
  • 23
0
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = layoutInflater.inflate(R.layout.add_recipe_pop,null);
final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
EditText recipe_url = (EditText)popupView.findViewById(R.id.recipe_url_text);
popupWindow.update();
popupWindow.showAtLocation(v,Gravity.CENTER,0,0);

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#E6E6E6"
android:focusable="true"   // toggle this when testing
android:orientation="vertical" >

<EditText
 android:id="@+id/recipe_url_text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:hint="@string/add_recipe_hint"
 android:focusable="true"
 android:focusableInTouchMode="true"
 android:inputType="textUri"
/>

this should work..let me know if it works, so i give my reason for it..

Elltz
  • 10,730
  • 4
  • 31
  • 59
  • It doesn't work :/ I tried toggling the focusable in the linearlayout and that didn't anything and if I take out the `popupWindow.setFocusable(true)` then I am not able to see a keyboard and the contextmenu still does not appear. Do you think having additional buttons and clicklisteners for those buttons in the popupWindow would affect it? Because I have 2 buttons with listeners on them... – Justin Tan Dec 28 '14 at 02:37
  • yea, the idea was, to keep focus out of the viewgroup, so when you click the edittext before the soft input keyboard pops up, and also no i do no think listeners will prevent that..@JustinTan – Elltz Dec 28 '14 at 03:09
  • oh..no.. ima try a sample that works and send it to you here, so, yeah @JustinTan – Elltz Dec 28 '14 at 10:12
  • Hey @Elltz, I did some more digging and I think this is just a bug in Android. Thank you for all your help but I think I'll just implement it some other way (probably a dialogframent). – Justin Tan Dec 28 '14 at 20:30
0

The key point of this problem is that when setText(), EditText must be added to Window to support selection.

This is key source code: Editor.prepareCursorControllers()

void prepareCursorControllers() {
    boolean windowSupportsHandles = false;

    ViewGroup.LayoutParams params = mTextView.getRootView().getLayoutParams();
    // If mTextView is not added to the window, params will be normal LayoutParams(FrameLayout.LayoutParams/LinearLayout.LayoutParams ect.)
    if (params instanceof WindowManager.LayoutParams) {
        WindowManager.LayoutParams windowParams = (WindowManager.LayoutParams) params;
        windowSupportsHandles = windowParams.type < WindowManager.LayoutParams.FIRST_SUB_WINDOW
                || windowParams.type > WindowManager.LayoutParams.LAST_SUB_WINDOW;
    }

    boolean enabled = windowSupportsHandles && mTextView.getLayout() != null;
    mInsertionControllerEnabled = enabled && isCursorVisible();
    mSelectionControllerEnabled = enabled && mTextView.textCanBeSelected();
    /// ...
}
ipcjs
  • 2,082
  • 2
  • 17
  • 20