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.

Geras
  • 21
  • 4
  • Possible duplicate of [How to stop Soft keyboard showing automatically when focus is changed (OnStart event)](https://stackoverflow.com/questions/5221622/how-to-stop-soft-keyboard-showing-automatically-when-focus-is-changed-onstart-e) – Ricardo A. Oct 28 '19 at 18:55
  • You can disable them all using `editText.setEnabled(false)` – Shermano Oct 28 '19 at 20:03
  • @Shermano I want the user to be able to select the text, doing this will not be possible. Anyway, thanks! I already found a solution. – Geras Oct 28 '19 at 21:05

2 Answers2

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" />
Mahabub Karim
  • 600
  • 7
  • 14
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.

The_Martian
  • 3,083
  • 2
  • 26
  • 50