4

I have an application where I needed to create a custom keyboard since barcode scanners are classified as hardware keyboards and hardware keyboards disable soft keyboards. The issue is that when no scanner is connected, the built in soft keyboard will be displayed when it's not needed. I have a button to show the custom keyboard which will also hide the default keyboard using

((InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(advText.getWindowToken(), 0);

(advText is an extended version of EditText)

I have tried placing that line of code in the onFocusChanged method of the EditText but nothing happens. If I use setInputType(InputType.TYPE_NULL); the android keyboard never shows, but the EditText doesn't display a cursor or anything that is typed from the custom keyboard (but I do know that keys are being stored since my "done" key sends the text from the EditText elsewhere just fine).

I'm fine with disabling the android keyboard completely for this app, just as long as the EditTexts show cursors and custom keyboard is only shown when using the button.

I have looked at these answers, but no luck finding a solution.

Close/hide the Android Soft Keyboard

How to show soft-keyboard when edittext is focused

How to hide Android soft keyboard on EditText

Edit:

My current solution is to run the hide method inside of the EditText's onCheckIsTextEditor since that seems to run after onFocusChanged, and it seems to be called about every second. But this is a nasty hack since the keyboard still shows for a split second and moves my layouts back and forth. My current test devices consist of the Motorola Photon Q 4G LTE with 4.1.2 and a Honeywell Dolphin 70e Black with 4.0.3

Community
  • 1
  • 1
Will
  • 146
  • 1
  • 12

2 Answers2

0

EditText provides this functionality with the flag textIsSelectable in EditText set to true. With this, the cursor will still be present, and you'll be able to select/copy/cut/paste, but SoftKeyboard will never show. Requires API 11 and above.

You can set it in your xml layout like this:

<EditText
    ...
    android:textIsSelectable="true"/>

Or programmatically, like this:

EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);

For anyone using API 10 and below, hack is provided here : https://stackoverflow.com/a/20173020/7550472

Community
  • 1
  • 1
Kaushik NP
  • 6,188
  • 8
  • 29
  • 57
-1

Edit your <activity> tag in your AndroidManifest.xml and add this attribute: android:windowSoftInputMode="stateAlwaysHidden" see http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

Just added that and remove all the other weird things you are doing to hide the soft keyboard.

P.S. You can also enable and disable this feature at runtime. getWindow().getAttributes().softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN;

Simon
  • 9,933
  • 44
  • 46