A hack to catch soft keyboard show/hide event on Android

Well, there are some workarounds that were already posted but they are incomplete (some work on 4.0.4 but not 2.3, some don’t work all the time), but at least they are still useful, they gave me an idea to make this “complete” workaround (well, at least it works like a charm in my case for now, I tested on both Android 2.3 and 4.0.4), so I would like to share it here, hope it helps someone out there 😉
Usage: just use following LinearLayout overridden class as the main layout(and of course, use“match_parent” for its layout_width and layout_height) of the activity where you want to catch soft keyboard show/hide events, then setOnSoftKeyboardVisibilityChangeListener for your main layout. Finally, you need this line in your <activity> tag (in manifest.xml) where SoftKeyboardHandledLinearLayout is used:


Take a look at following code to get a better idea of how to implement this hack.



public class SoftKeyboardHandledLinearLayout extends LinearLayout {
	private boolean isKeyboardShown;
	private SoftKeyboardVisibilityChangeListener listener;

	public SoftKeyboardHandledLinearLayout(Context context) {

	public SoftKeyboardHandledLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);

	public SoftKeyboardHandledLinearLayout(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);

	public boolean dispatchKeyEventPreIme(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			// Keyboard is hidden <<< RIGHT
			if (isKeyboardShown) {
				isKeyboardShown = false;
		return super.dispatchKeyEventPreIme(event);
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
    	final int actualHeight = getHeight();
    	if (actualHeight > proposedheight) {
			// Keyboard is shown
			if (!isKeyboardShown) {
				isKeyboardShown = true;
    	} else {
	        // Keyboard is hidden <<< this doesn't work sometimes, so I don't use it
    	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	public void setOnSoftKeyboardVisibilityChangeListener(SoftKeyboardVisibilityChangeListener listener) {
		this.listener = listener;
	// Callback
	public interface SoftKeyboardVisibilityChangeListener {
		public void onSoftKeyboardShow();
		public void onSoftKeyboardHide();





SoftKeyboardHandledLinearLayout mainView = (SoftKeyboardHandledLinearLayout) findViewById(R.id.main_view);
		new SoftKeyboardVisibilityChangeListener() {
			public void onSoftKeyboardShow() {
				// TODO: do something here
			public void onSoftKeyboardHide() {
				// TODO: do something here



Originally From: http://tech.leolink.net/2014/02/a-hack-to-catch-soft-keyboard-showhide.html


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s