Disable Soft-Keyboard from Edittext But Still Allow Copy/Paste

Disable soft-keyboard from EditText but still allow copy/paste?

After hours and hours of research, I finally found a solution that works for all API versions. Hope this saves someone's time.

If you are developing for API >= 11, the solution is simple, either:

1) Add the two properties below in the xml file of EditText

android:inputType="none"
android:textIsSelectable="true"

or

2) Programatically do the below

myEditText.setInputType(InputType.TYPE_NULL);
myEditText.setTextIsSelectable(true);

And you're done.

If you want to cater for API < 11 as well, I found that there is no way to disable to keyboard from popping out if you wanted to select the text for copy paste purpose. Setting focusable to false will disable the keyboard but it doesn't help because it disables your ability to select text too. Any other solutions I found in stackoverflow all either doesn't work or disables text selection at the same time too.

One ugly way to solve this is as such..

First, add this property in the xml file of EditText

android:editable="false"

Yes this is deprecated, but necessary for making the EditText not editable in API version < 11.

Next, we will need to hide the keyboard as soon as it shows up, so that we can continue selecting text without the keyboard blocking the way.

Use this code below to detect keyboard showing up (solution obtained from https://stackoverflow.com/a/9108219/1241783), and hide it immediately.

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
{
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);

int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...

//Hide the keyboard instantly!
if (getCurrentFocus() != null)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
}
});
}

It works for my case. Though you can see the keyboard showing up in a split second (which is the ugly part) but I can't think of any other way to get this to work at the time of writing. If you have a better solution, please leave a comment!

Let me know too if this saves someone's time :)

Disable keyboard on EditText

Here is a website that will give you what you need

As a summary, it provides links to InputMethodManager and View from Android Developers. It will reference to the getWindowToken inside of View and hideSoftInputFromWindow() for InputMethodManager

A better answer is given in the link, hope this helps.

here is an example to consume the onTouch event:

editText_input_field.setOnTouchListener(otl);

private OnTouchListener otl = new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
return true; // the listener has consumed the event
}
};

Here is another example from the same website. This claims to work but seems like a bad idea since your EditBox is NULL it will be no longer an editor:

MyEditor.setOnTouchListener(new OnTouchListener(){

@Override
public boolean onTouch(View v, MotionEvent event) {
int inType = MyEditor.getInputType(); // backup the input type
MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
MyEditor.onTouchEvent(event); // call native handler
MyEditor.setInputType(inType); // restore input type
return true; // consume touch even
}
});

Hope this points you in the right direction

How to disable copy/paste from/to EditText

If you are using API level 11 or above then you can stop copy,paste,cut and custom context menus from appearing by.

edittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}

public void onDestroyActionMode(ActionMode mode) {
}

public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});

Returning false from onCreateActionMode(ActionMode, Menu) will prevent the action mode from being started(Select All, Cut, Copy and Paste actions).

How to disable keypad popup when on edittext?

The best solution lies in the Project Manifest file (AndroidManifest.xml), add the following attribute in the activity construct

android:windowSoftInputMode="stateHidden"


Example:

    <activity android:name=".MainActivity" 
android:windowSoftInputMode="stateHidden" />

Description:

  • The state of the soft keyboard — whether it is hidden or visible — when the activity becomes the focus of user attention.
  • The adjustment made to the activity's main window — whether it is resized smaller to make room for the soft keyboard or whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.

Introduced in:

  • API Level 3.

Link to the Docs

Note:
Values set here (other than "stateUnspecified" and "adjustUnspecified") override values set in the theme.

Android: Disable soft keyboard at all EditTexts

create your own class that extends EditText and override the onCheckIsTextEditor():

public class NoImeEditText extends EditText {
public NoImeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onCheckIsTextEditor() {
return false;
}
}

how to block virtual keyboard while clicking on edittext in android?

Here is a website that will give you what you need.

As a summary, it provides links to InputMethodManager and View from Android Developers. It will reference to the getWindowToken inside of View and hideSoftInputFromWindow() for InputMethodManager.

A better answer is given in the link, hope this helps.

EDIT

From the link posted above, here is an example to consume the onTouch event:

editText.setOnTouchListener(otl);

private OnTouchListener otl = new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
return true; // the listener has consumed the event
}
};

Here is another example from the same website. This claims to work but seems like a bad idea since your EditBox is NULL it will be no longer an editor:

myEditor.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int inType = myEditor.getInputType(); // backup the input type
myEditor.setInputType(InputType.TYPE_NULL); // disable soft input
myEditor.onTouchEvent(event); // call native handler
myEditor.setInputType(inType); // restore input type
return true; // consume touch event
}
});

Hope this points you in the right direction!



Related Topics



Leave a reply



Submit