4

To make edittext selectable without showing soft keyboard, I had put edittext.textIsSelectable(true) attribute and I had hidden the soft keyboard.

When I try to open soft keyboard again, it not opening.

I tried setting setFocusable(true) in edittext but still keyboard not opening. Any suggestions would be appreciated.

Thank you

Vijay
  • 545
  • 1
  • 5
  • 15

3 Answers3

3

By using the below code for EditText you can get the event for Cut/Copy/Paste And you have to create editText like below.And you can hide the soft keyboard after event fired...

<com.example.textlink.EditTextMonitor
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:hint="EditText" />

Create one java file in your package...

public class EditTextMonitor extends EditText {
private final Context mcontext; // Just the constructors to create a new
                                // EditText...

public EditTextMonitor(Context context) {
    super(context);
    this.mcontext = context;
}

public EditTextMonitor(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mcontext = context;
}

public EditTextMonitor(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.mcontext = context;
}

@Override
public boolean onTextContextMenuItem(int id) {
    // Do your thing:
    boolean consumed = super.onTextContextMenuItem(id);
    // React:
    switch (id) {
    case android.R.id.cut:
        onTextCut();
        break;
    case android.R.id.paste:
        onTextPaste();
        break;
    case android.R.id.copy:
        onTextCopy();
    }
    return consumed;
}

/**
 * Text was cut from this EditText.
 */
public void onTextCut() {
    InputMethodManager imm = (InputMethodManager) getContext()
            .getApplicationContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    Toast.makeText(mcontext, "Event of Cut!", Toast.LENGTH_SHORT).show();
}

/**
 * Text was copied from this EditText.
 */
public void onTextCopy() {
    InputMethodManager imm = (InputMethodManager) getContext()
            .getApplicationContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    Toast.makeText(mcontext, "Event of Copy!", Toast.LENGTH_SHORT).show();
}

/**
 * Text was pasted into the EditText.
 */
public void onTextPaste() {
    InputMethodManager imm = (InputMethodManager) getContext()
            .getApplicationContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    Toast.makeText(mcontext, "Event of Paste!", Toast.LENGTH_SHORT).show();
}}
santhoshkumar
  • 191
  • 2
  • 10
  • Hi Santhosh, Thanks for your reply. I really appreciate it. But the problem statement is when we select some text in edittext soft keyboard should not open. In your case after selecting cut, copy or paste only we can able to hide the soft keyboard. But in my case keyboard should be hidden before selecting cut,copy or paste. Thank you. – Vijay Oct 06 '16 at 15:48
1

Without setting attribute edittext.textIsSelectable(true)

you can able to select the entered text and copy it right, why don't you go blindly with the edittext with basic attributes

santhoshkumar
  • 191
  • 2
  • 10
  • I don't want the keyboard to be open when I select some text in edittext that's why. I think this is the only way to hide keyboard without affecting selection. If you know some other method to hide the keyboard when selecting the text, you are welcome to answer. :-) – Vijay Oct 06 '16 at 13:51
  • I removed textSelectable and now it is working. Also text is select able without this property. – Rasel Feb 02 '17 at 05:04
1

I found the answer.

Just override the isTextSelectable() method in Edittext.

If you want to open keyboard then return super.isTextSelectable().

If you don't want to open keyboard then return true.

That's it. It worked for me.

Vijay
  • 545
  • 1
  • 5
  • 15