2

I have a difficult situation. I need to hide certain views when softkeyboard is displayed and show it back when softkeyboard hides.

I have tried different approaches where I detect (by workaround) the visibility of the softkeyboard and show/hide views accordingly but the transactions are not smooth and the whole layout is flickered when doing show.

Below is something similar to what I want.

softkeyboard hidden softkeyboard visible

Is there any other alternate of doing similar without having to workaround with visibility of softkeyboard?

Thanks.

Vinod Maurya
  • 3,957
  • 10
  • 47
  • 79
  • From the looks of the pictures you wish to disable the softkeyboard while the custom view for input is in use? – cYrixmorten Feb 24 '15 at 12:56
  • can you post your current implementation? – SMR Feb 24 '15 at 12:59
  • @SMR the current implementation is based on answer from http://stackoverflow.com/questions/25216749/softkeyboard-open-and-close-listener-in-an-activity-in-android – Vinod Maurya Feb 25 '15 at 05:34

3 Answers3

0

I think you might want to turn the things around and instead of detecting visibility of softkeyboard, then take control of it.

The code I am using for this is:

public static void showKeyboard(Activity activity) {
    if (activity == null || activity.getCurrentFocus() == null)
        return;

    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    // will only trigger if no physical keyboard is open
    inputMethodManager.showSoftInput(activity.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);
}

public static void hideKeyboard(Activity activity) {
    if (activity == null || activity.getCurrentFocus() == null)
        return;

    InputMethodManager imm = (InputMethodManager) activity.getSystemService(
            Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

I also have a third method for hiding the keyboard. This was needed in a dialog showing an edittext to force the keyboard to close when 'Ok/cancel' is clicked:

public static void hideKeyboard(Context c, IBinder windowToken) {
    InputMethodManager mgr = (InputMethodManager) c
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(windowToken, 0);
}

Usage:

Util.hideKeyboard(getActivity(), editText.getWindowToken());
cYrixmorten
  • 7,032
  • 3
  • 22
  • 33
0

Without knowing what "certain views" means in more detail it's hard to answer. Can you utilize any of the soft input modes pan/resize etc or do you need more custom behaviour?

You CAN use workaround tricks such as an OnGlobalLayoutListener on your view root, but it is costly since it's called very often. There are methods on the InputMethodManager to request the IME to be shown/hidden, with callbacks invoked with the result, but it is not always reliable. Your use case is a pretty commonly described problem, but depending on what exactly you are trying to accomplish it's hard to recommend anything.

JHH
  • 6,514
  • 5
  • 28
  • 64
  • "Certain Views" are View Pager and Text View. In the above image where keyboard is not visible, note the there is a TextView (below edit text) and a view pager (above edit text). In the second image where keyboard is visible, the TextView and ViewPager are not visible. – Vinod Maurya Feb 25 '15 at 05:32
0

I solved a similar problem by overriding the EditText and adding an interface OnCloseSoftKeyboardListener to it, which is invoked when the softkeyboard is closed:

public class MyEditText extends EditText {

private OnCloseSoftKeyboardListener mBackButtonListener;
private OnTouchListener mTouchListener;

// HANDLING OF SOFT KEYBOARD BACK BUTTON
public void setOnCloseSoftKeyboardListener(OnCloseSoftKeyboardListener callback) {
    mBackButtonListener = callback;
}


/**
 * Overrides the handling of the back key to move fields or whatever, instead of dismissing the input method.
 */
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
    if (mBackButtonListener != null && event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
        if (mBackButtonListener.onCloseSoftKeyboard(this)) {
            return true;
        }
    }

    return super.dispatchKeyEventPreIme(event);
}


public interface OnCloseSoftKeyboardListener {

    /**
     * @param myEditText 
     * @return true if event was consumed, false otherwise
     */
    boolean onCloseSoftKeyboard(EditText view);
}
}

In your activity you implement the interface:

public class MyActivity implements OnCloseSoftKeyboardListener {

MyEditText mTextView; 
(...)

mTextView.setOnCloseSoftKeyboardListener(this);
(...)

@Override
public boolean onCloseSoftKeyboard(EditText view) {

    // do what you need to do...

    return false;
}

If you also add an OnTouchListener to the EditText you can handle both open and close of softKeyboard.

botzek13
  • 91
  • 5