javaandroidandroid-edittextfocusable

EditText with selectable text is unfocusable


My app contains a popup with an EditText. I want the user to be able not to only type and delete text, but to also copy & paste from and to the EditText box.

After searching online I found that the following xml tag should do the trick.

android:textIsSelectable="true"

I added the line but then I can't open the keyboard by clicking on the EditText box.

I found many solutions that open the keyboard explicitly but that isn't what I'm looking for, I want it to open on a click and to allow copying and pasting text with a long click, just like an EditText in the browser, for example.

Adding one of the following doesn't work as well:

<requestFocus />

edit_text.requestFocus();

xml:

        <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="true"
        android:text=""
        android:textSize="25sp"
        android:gravity="center_horizontal"
        android:inputType="text"
        android:layout_weight="0.1"
        android:id="@+id/editTextPopup_input"
        android:layout_marginEnd="20sp" />

Java:

    public static void inflateEditTextPopup(int parent_id, String content_text, String edit_text_text, String act_button_text)
{
    LayoutInflater layoutInflater = (LayoutInflater)Main.main_activity.getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    final View popupView = layoutInflater.inflate(R.layout.edit_text_popup, null);
    final PopupWindow window = new PopupWindow(popupView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    window.setFocusable(true);
    final EditText input = (EditText)popupView.findViewById(R.id.editTextPopup_input);
    input.setText(edit_text_text);
    input.setFocusable(true);
    input.requestFocus();

    TextView content = (TextView)popupView.findViewById(R.id.editTextPopup_content);
    content.setText(content_text);

    Button btnClose = (Button)popupView.findViewById(R.id.editTextPopup_cancel);
    btnClose.setOnClickListener(new Button.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            window.dismiss();
        }
    });

    Button btnAct = (Button)popupView.findViewById(R.id.editTextPopup_ok);
    btnAct.setText(act_button_text);
    btnAct.setOnClickListener(new Button.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            handleEditTextPopupInput(window, input.getText().toString());
            window.dismiss();
        }
    });

    LinearLayout parent = (LinearLayout)Main.main_activity.findViewById(parent_id);
    window.showAsDropDown(parent);
}

logcat:

W/TextView: TextView does not support text selection. Selection cancelled.

Solution

  • After a lot of searching online I found that there is no answer, because this is a bug in Android, an EditText is not selectable from a PopupWindow.

    In my case I used something like a "Paste" button, which is not very comfortable, but good enough for me.