Here's the solution which will hide the keyboard from anywhere.
1st create in your activity of choice the listener for the state and the method that will do the closing (based on the open state).
public class MainActivity extends SherlockFragmentActivity {
private boolean mKeyboardOpen = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// add "keyboard open listener"
final View v = findViewById(R.id.pager);
v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int hRoot = v.getRootView().getHeight();
int hView = v.getHeight();
int heightDiff = hRoot - hView;
// if more than 150 pixels, its probably a keyboard...
mKeyboardOpen = heightDiff > 150;
Log.d(TAG, "hRoot=" + hRoot + ", hView=" + hView + ", mKeyboardOpen=" + mKeyboardOpen);
}
});
}
public void closeSoftKeyboard() {
if (mKeyboardOpen) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
}
2nd call ((MainActivity) getActivity()).closeSoftKeyboard(); from anywhere, e.g. your EditText's OnClickListener().
Hint: I'm using the ViewPager root view (R.id.pager), but you should probably replace it with your view root id.