Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android soft keyboard will not show in 2.2/2.3, but does in 3.0+

My app works on Android 2.2 and later. In it, I use ActionbarSherlock to allow the action bar usage for pre-3.0 devices. I used an EditText in the action bar to allow user text input for search.

Using the emulator, with Android 4.0 and 4.1 (I have not tried 3.x because it's not a tablet app), when the EditText is selected, the soft keyboard pops up as desired. But not so using Android 2.2 or 2.3.3, it just does not display.

The code for the EditText is straightforward:

item.setActionView(R.layout.collapsible_edittext);
etInput = (EditText) item.getActionView().findViewById(R.id.etInput);   
etInput.requestFocus();

Layout:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/etInput"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:lines="1" 
    android:maxLines="1"
    android:inputType="textFilter|textNoSuggestions"
    android:imeOptions="actionSend"/>

Now I've tried specifically showing the soft keyboard using this snippet immediately after etInput.requestFocus();, but it made no difference:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etInput, InputMethodManager.SHOW_IMPLICIT);

I'm trying to figure out if this is an issue with ActionbarSherlock or a more general Android issue. I've scoured the many articles on forcing soft keyboard display in an Activity, but have not yet found the solution.

Thanks

like image 917
mraviator Avatar asked Aug 06 '12 12:08

mraviator


1 Answers

I had the same problems... everything worked fine until i tested on HTC Nexus One. I ended up adding the following code to the onActionExpandListener attached to the AciontBarSherlock menuItem.

item.setOnActionExpandListener(new OnActionExpandListener() {

    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
    // post delayed to allow time for edittext to become visible
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
        mSearchText.clearFocus();
        showKeyboard();         
        mSearchText.requestFocus();
        }
    }, 400);

    return true;
    }

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
    hideKeyboard();
    return true;
    }
});

private void showKeyboard() {
 if (android.os.Build.VERSION.SDK_INT < 11) {
    mInputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
 } else {
    mInputManager.showSoftInput(mSearchText, InputMethodManager.SHOW_IMPLICIT);
 }
}

private void hideKeyboard() {
 if (android.os.Build.VERSION.SDK_INT < 11) {
   mInputManager.hideSoftInputFromWindow(getActivity().getWindow().getCurrentFocus().getWindowToken(),0);      
 } else {
    mInputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
 }  
}
like image 157
lwegener Avatar answered Sep 30 '22 16:09

lwegener