Reputation: 3
I have an AutoCompleteTextView as Collabsible ActionItem, and i want the keyboard to be displayed when it gets expanded and focussed. This is my code in onCreateOptionsMenu():
menu.add("Search")
.setIcon(R.drawable.ic_search)
.setActionView(R.layout.collapsible_edittext)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
final MenuItem searchMenuItem = menu.getItem(0);
searchMenuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) item
.getActionView();
autoCompleteTextView
.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.d(TAG, "onFocusChange: " + hasFocus);
if (hasFocus) {
mInputManager.showSoftInput(v,
InputMethodManager.SHOW_FORCED);
} else {
mInputManager.hideSoftInputFromWindow(
v.getWindowToken(), 0);
}
}
});
autoCompleteTextView.requestFocus();
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) item
.getActionView();
autoCompleteTextView.clearFocus();
autoCompleteTextView.setText("");
return true;
}
});
When I click on the ActionItem the first time, no keybord is shown. After collapsing and then clickin on the Item the keyboard gets shown. But I want the keyboard to be shown the first time the user clicks on the action item. I'm using ActionBarSherlock if that matters. So why is the keyboard not shown on first expansion? Any ideas?
Upvotes: 0
Views: 986
Reputation: 2228
Have you tried manually setting the focus to the edit text and manually showing the keyboard in the onOptionsItemSelected method?
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_SEARCH:
// select edittext here
// show keyboard here
return true;
}
return super.onOptionsItemSelected(item);
}
EDIT: I found this code to show the keyboard from here:
I tested it on my app and it works...
public static void setKeyboardFocus(final EditText primaryTextField) {
(new Handler()).postDelayed(new Runnable() {
public void run() {
primaryTextField.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
primaryTextField.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
}
}, 100);
}
Upvotes: 1