27

I am developing for a tablet using Android 2.2.

I have a form which I am using for new and editable instances. When editable, I want to stop the user from editing certain fields. I manage this in my onStart-event, setting the txt.setFocusableInTouchMode(false). This forces the focus to the next focusable EditText in my form (which is great), but when running, the soft keyboard automatically appears for the EditText with the focus. Anybody know how to stop this?

Here's the code (called in the onStart event):

private void PopulateFields(){
    TextView txtTitle = (TextView) this.findViewById(R.id.txtEquipmentEditTitle);
    AutoCompleteTextView txtMake = (AutoCompleteTextView) this.findViewById(R.id.autEquipmentEditMake);
    AutoCompleteTextView txtModel = (AutoCompleteTextView) this.findViewById(R.id.autEquipmentEditModel);
    EditText txtDesc = (EditText) this.findViewById(R.id.txtEquipmentEditDesc);
    TextView txtDeptKey = (TextView) this.findViewById(R.id.txtEquipmentEditDeptKey);
    EditText txtSerial = (EditText) this.findViewById(R.id.txtEquipmentEditSerialNo);
    EditText txtBarCode = (EditText) this.findViewById(R.id.txtEquipmentEditBarCode);

    txtTitle.setText("Edit Equipment");
    txtMake.setText(make);
    txtModel.setText(model);
    txtDesc.setText(desc);
    txtDeptKey.setText(Integer.toString(deptKey));
    txtSerial.setText(serial);
    txtBarCode.setText(barCode);
    txtMake.setEnabled(false);
    txtModel.setEnabled(false);
    txtDesc.setEnabled(false);
    txtMake.setClickable(false);
    txtMake.setFocusableInTouchMode(false);
    txtModel.setFocusableInTouchMode(false);
    txtDesc.setFocusableInTouchMode(false);
    txtMake.setFocusable(false);
    txtModel.setFocusable(false);
    txtDesc.setFocusable(false);
}
Milad Faridnia
  • 8,038
  • 13
  • 63
  • 69
Rune Borgen
  • 359
  • 1
  • 4
  • 11

4 Answers4

65

Maybe this will help you:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Put this method in the onCreate() method of your activity when the activity is first created or called.

Jose Mhlanga
  • 468
  • 5
  • 12
MobileCushion
  • 6,816
  • 5
  • 37
  • 61
  • This fixed my problem for the xoom (Android 3.0.1). I added the above line to my onCreate() method. I'm still not sure what a "soft input" is. – cyber-monk Mar 31 '11 at 23:06
  • 2
    Soft input is the onscreen keyboard. The alternative is a real physical keyboard. – Ed Burnette Sep 07 '11 at 19:44
  • 3
    This does not seem to have any effect on Android 2.2.Neither does SOFT_INPUT_STATE_ALWAYS_HIDDEN. – Torben Jan 05 '12 at 18:55
  • 2
    You can also achieve the same effect by adding android:windowSoftInputMode="stateHidden" on your activity in the manifest – AndroidGeek Jul 03 '14 at 11:29
13

We can Hide the device Keybord by following ways which totally depends on the need of situation_

First Way_

EditText userId;
    /*
     * When you want that Keybord not shown untill user clicks on one of the EditText Field.
     */
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Second Way_ InputMethodManager

    /*
     * When you want to use service of 'InputMethodManager' to hide/show keyboard
     */
    InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    inputManager.hideSoftInputFromWindow(userId.getWindowToken(),
            InputMethodManager.HIDE_NOT_ALWAYS);

Third Way_

    /*
     * Touch event Listener
     * When you not want to open Device Keybord even when user clicks on concern EditText Field.
     */
    userId.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int inType = userId.getInputType(); // backup the input type
            userId.setInputType(InputType.TYPE_NULL); // disable soft input
            userId.onTouchEvent(event); // call native handler
            userId.setInputType(inType); // restore input type
            userId.setFocusable(true);
            return true; // consume touch even
        }
    });

Behalf of all of the above ways_ you can also define windowSoftInputMode in applicationsmanifest` file_

<manifest ... >
 <application ... >
    <activity 
        android:windowSoftInputMode="stateHidden|stateAlwaysHidden"
        ... >
        <intent-filter>
           ...
        </intent-filter>
    </activity>

 </application>
</manifest>

I hope this will help all!

Rupesh Yadav
  • 14,336
  • 5
  • 53
  • 68
6

If you want this for a particular EditText this will work.

editText.setShowSoftInputOnFocus(false);

Shivam Dawar
  • 343
  • 4
  • 6
-1

this is the answer: In your AndroidManifest.xml add an attribute to the Activity:

        <activity android:name=".MyActivity" android:windowSoftInputMode="adjustPan"> </activity>