The Android software keyboard will not appear in version 2.2 / 2.3, but in version 3.0+

My application runs on Android 2.2 and later. In it, I use ActionbarSherlock to allow the use of the action bar for devices up to 3.0. I used EditText in the action bar to allow the user to enter search text.

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

The code for EditText is simple:

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

Markup:

 <?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 tried to specially show the soft keyboard with this fragment right after etInput.requestFocus(); but it didn’t matter:

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

I am trying to figure out if this is a problem with ActionbarSherlock or a more general problem with Android. I looked through a lot of articles on how to cause a soft keyboard to appear in Activity, but have not yet found a solution.

thanks

+7
source share
2 answers

I had the same problems ... everything worked fine until I tested HTC Nexus One. I ended up adding the following code to onActionExpandListener attached to an AciontBarSherlock menu item.

 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); } } 
+4
source

For those working in C sharp on a Mono droid, this is the code:

  new Thread(new ThreadStart(() => { while (DateTime.Now < _dt) Thread.Sleep(10); RunOnUiThread(showKeyboard); } )).Start(); protected void showKeyboard() { int osSDK = (int)Android.OS.Build.VERSION.SdkInt; if (osSDK < 11) { this.mm().ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly); } else { EditText editTextView = FindViewById<EditText>(Resource.Id.EditTextSubCellAddVenue); this.mm().ShowSoftInput(editTextView, ShowFlags.Implicit); } } protected void hideKeyboard() { int osSDK = (int)Android.OS.Build.VERSION.SdkInt; if (osSDK < 11) { this.mm().HideSoftInputFromWindow(this.Window.CurrentFocus.WindowToken, 0); } else { this.mm().HideSoftInputFromWindow(this.CurrentFocus.WindowToken, HideSoftInputFlags.NotAlways); } } protected InputMethodManager mm() { if (imm == null) { EditText editTextView = FindViewById<EditText>(Resource.Id.EditTextSubCellAddVenue); imm = (InputMethodManager)this.GetSystemService(Android.Content.Context.InputMethodService); } return imm; } 
0
source

All Articles