0

How do I hide the keyboard in the activity and prevent it from opening even by clicking an edittext (programmatically)?

I HAVE ALREADY SOLVED: I used this code here in the onCreate event:

edittext1.setShowSoftInputOnFocus(false);

This will disable the keyboard in edittext without interfering with the picker or cursor.

3

2 Answers 2

0

Hide keyboard in onCreate() method of activity

/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
   if(getCurrentFocus()!=null) {
       InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
   }
}

or simply use this ("android:windowSoftInputMode="stateHidden") in AndroidManifest.xml file

<activity
 android:name="com.example.stockquote.StockInfoActivity"
 android:windowSoftInputMode="stateHidden
 android:label="@string/app_name" />
2
  • I don't want the keyboard to open when I click edittext
    – Geras
    Oct 28, 2019 at 17:13
  • Add below properties to the Edittext controller in the layout file <Edittext android:focusableInTouchMode="true" android:cursorVisible="false" android:focusable="false" /> Oct 29, 2019 at 3:31
0

There are two ways to achieve this:

In manifest do the following:

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

Or in your java code do the following:

View view = this.getCurrentFocus();
    if (view != null) {  
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

Please refer to this SO answer for detailed explanation.

2
  • I don't want the keyboard to open when I click edittext
    – Geras
    Oct 28, 2019 at 17:14
  • @MahabubKarim Ready!
    – Geras
    Oct 29, 2019 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.