Problem:
I want to hide the keyboard when the button "Add" is pressed. There are two
EditText
on the screen. The keyboard doesn't appear at starting the activity, which is good, but it doesn't go away on clicking button.
Here are all the possible questions from Stack Overflow that I have seen whose answers don't help me:
Close/hide the Android Soft Keyboard
Programmatically Hide/Show Android Soft Keyboard
How to hide Soft Keyboard when activity starts
How to hide soft keyboard on android after clicking outside EditText?
and many others.
Here is my code:
AddActivity
public class AddActivity extends ActionBarActivity {
EditText text1,text2;
DbHelper db;
ListView l;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
db = new DbHelper(this);
l = (ListView) findViewById(R.id.listInAddActivity);
text1 = (EditText) findViewById(R.id.i1);
text2 = (EditText) findViewById(R.id.i2);
// text1.setInputType(InputType.TYPE_NULL);
// text2.setInputType(InputType.TYPE_NULL);
hideKeyboard();
loadDataInAdd();
}
public void addNewTask(View view) {
String s1 = text1.getText().toString();
String s2 = text2.getText().toString();
db.addData(s1,s2);
loadDataInAdd();
hideKeyboard();
}
public void loadDataInAdd()
{
try {
Cursor cursor = db.fetchData();
ListAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.tasks,
cursor,
new String[]{db._ID, db.COLUMN_1, db.COLUMN_2},
new int[]{R.id.idnum, R.id.c1, R.id.c2}, 0);
l.setAdapter(myAdapter);
}
catch(NullPointerException e)
{
e.printStackTrace();
}
// MainActivity.loadData();
}
private void hideKeyboard() {
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
Most answers are about the InputManager
method, but it does not work for me, i.e., does not hide the keyboard when clicking on the button. Here is another variation of InputManager
I used:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
I also tried this:
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);
and this does not work either (keyboard isn't hidden on clicking button).
I have also tried:
<activity android:name=".AddActivity"
android:label="@string/app_name"
android:parentActivityName=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
and
<activity android:name=".AddActivity"
android:label="@string/app_name"
android:parentActivityName=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
With this one, my app stopped working:
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
This one totally hid the keyboard (the keyboard did not appear even when the editText
was clicked):
text1.setInputType(InputType.TYPE_NULL);
text2.setInputType(InputType.TYPE_NULL);
Here pass HIDE_IMPLICIT_ONLY at the position of showFlag and 0 at the position of hiddenFlag . It will forcefully close soft Keyboard.
Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.
I decided to use an onclicklistener
for my button, instead of using another function and calling it via onClick
in AddActivity.xml
.
I was halfway through my question when I decided to try using OnCliCkListener
once again. After several random tries, the following is the final code that works for me:
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
});
Another way is to use OnClickListener
in the following manner:
private View.OnClickListener mListener = new View.OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
//public void onClick(View v) {
try {
InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
catch (Exception e) {
e.printStackTrace();
}
}
};
Important points for beginners like me:
protected void onCreate(Bundle savedInstanceState)
To call the above, place this code in protected void onCreate(Bundle savedInstanceState)
:
btn.setOnClickListener(mListener);
Use try-catch
for exception handling in both methods (onClickListener
, setOnClickListener
)
Upon searching for why onClick
, the XML
attribute doesn't work for me, while OnClickListener
does, I have found the following links useful:
setOnclickListener vs OnClickListener vs View.OnClickListener
Android onClick in XML vs. OnClickListener
How exactly does the android:onClick XML attribute differ from setOnClickListener?
Difference between OnClick() event and OnClickListener?
Now, halfway through my answer, I realized my mistake in using the onClick
from XML. Here is what I did to get rid of the OnClickListener
s:
onClick
. Here in my case, I moved the entire code inside hideKeyboard()
to addNewTask
.Some important points regarding using onClick
The method should be public
and void
.
It should take a View
parameter, such as View V
.
Finally, the code that works:
public void addNewTask(View view) {
String s1 = text1.getText().toString();
String s2 = text2.getText().toString();
db.addData(s1, s2);
loadDataInAdd();
// hideKeyboard(); below is the code to hide keyboard
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Summary
I found 3 ways to hide soft keyboard for my case, using:
OnClickListener :
private View.OnClickListener mListener = new View.OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
//public void onClick(View v) {
try {
InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
catch (Exception e) {
e.printStackTrace();
}
}
};
setOnClickListener:
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
});
onClick, the XML
attribute:
public void addNewTask(View view) {
String s1 = text1.getText().toString();
String s2 = text2.getText().toString();
db.addData(s1, s2);
loadDataInAdd();
// hideKeyboard();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
this will help you to hide your keyboard on startup until you touch the edit text.
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);
in your case, try this
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With