Tuesday, April 17, 2012

Android EditText, soft keyboard show/hide event?


Is it possible to catch the event that Soft Keyboard was shown or hidden for EditText?



Source: Tips4all

4 comments:

  1. Hi I'd used following workaround:

    As far as my content view is a subclass of LinearLayout (could be any other view or view group), I'd overridden onMeasure method lilke following:

    Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
    final int actualHeight = getHeight();

    if (actualHeight > proposedheight){
    // Keyboard is shown
    } else {
    // Keyboard is hidden
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    This workaround helped me to hide some controls when keyboard is showing and bring back otherwise.

    Hope this would be useful.

    ReplyDelete
  2. There actually isn't such an event to catch. The IME is simply showing and hiding its window; the feedback you get from this is the window manager causing your own window's content to resize if you have put it in resize mode.

    ReplyDelete
  3. We can toggle the on-screen keyboard using the code below.

    InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMgr.toggleSoftInput(0, 0);


    To show Soft Keyboard for EditText:

    EditText editText = (EditText) findViewById(R.id.myEdit);
    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    // only will trigger it if no physical keyboard is open
    mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);


    And to hide:

    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);


    Refer this links: http://www.androidpeople.com/android-hide-virtual-keyboard-through-code/ , http://stackoverflow.com/questions/1109022/how-to-close-hide-the-android-soft-keyboard

    ReplyDelete
  4. try these methods: showSoftInput(View, int, ResultReceiver) and hideSoftInputFromWindow(IBinder, int, ResultReceiver). You can override onReceiveResult(int resultCode, Bundle resultData) method of ResultReceiver class to handle show/hide event.

    ReplyDelete