I have an Activity with some EditText fields and some buttons as a convenience for what normally would be used to populate those fields. However when we the user touches on
You need to add the following attribute for the Activity
in your AndroidManifest.xml
.
<activity
...
android:windowSoftInputMode="stateHidden|adjustResize"
...
/>
Hello Use this code it is working 100% for me
EditText ee=new EditText(this);
ee.setShowSoftInputOnFocus(false);
*But you want to just hide keyboard on edittext click given below code will work but keyboard icon and upward sign of back-button will create *Given code is like this
EditText ee=new EditText(this);//create edittext
final View.OnTouchListener disable =new View.OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager img =
(InputMethodManager)v.getContext().getSystemService(INPUT_METHOD_SERVICE);
if (img != null) {
img.hideSoftInputFromWindow(v.getWindowToken(),0);
}
return true;
}
};
//call touch listener
ee.setOnTouchListener(disable);
Use also it in case of text-changing listener
EditText ee= new EditText(this);
Text=new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int
after) {
InputMethodManager img = (InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
img.hideSoftInputFromWindow(ee.getWindowToken(), 0);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
InputMethodManager img = (InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
img.hideSoftInputFromWindow(ee.getWindowToken(), 0);
}
@Override
public void afterTextChanged(Editable s) {
InputMethodManager img = (InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
img.hideSoftInputFromWindow(ee.getWindowToken(), 0);
}
};
ee.addTextChangedListener(Text);
Use it also in case of onclick listener
EditText ee=new EditText(this);
ee.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
InputMethodManager img= (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
img.hideSoftInputFromWindow(ee.getWindowToken(),0);
}});
All the code will work but for me first code is best
I sometimes use a bit of a trick to do just that. I put an invisible focus holder somewhere on the top of the layout. It would be e.g. like this
<EditText android:id="@id/editInvisibleFocusHolder"
style="@style/InvisibleFocusHolder"/>
with this style
<style name="InvisibleFocusHolder">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:inputType">none</item>
</style>
and then in onResume I would call
editInvisibleFocusHolder.setInputType(InputType.TYPE_NULL);
editInvisibleFocusHolder.requestFocus();
That works nicely for me from 1.6 up to 4.x
There seems to be quite a variety of ways of preventing the system keyboard from appearing, both programmatically and in xml. However, this is the way that has worked for me while supporting pre API 11 devices.
// prevent system keyboard from appearing
if (android.os.Build.VERSION.SDK_INT >= 11) {
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);
} else {
editText.setRawInputType(InputType.TYPE_NULL);
editText.setFocusable(true);
}
Hide the keyboard
editText.setInputType(InputType.TYPE_NULL);
Show Keyboard
etData.setInputType(InputType.TYPE_CLASS_TEXT);
etData.setFocusableInTouchMode(true);
in the parent layout
android:focusable="false"
The following line is exactly what is being looked for. This method has been included with API 21
, therefore it works for API 21
and above.
edittext.setShowSoftInputOnFocus(false);