Reputation: 21
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.
Upvotes: 0
Views: 71
Reputation: 3767
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.
Upvotes: 0
Reputation: 870
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" />
Upvotes: 0