I am using ActionBarSherlock library to get actionbar on pre-Honeycomb versions. I have an activity for which the actionbar menu is inflated from below xml
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_bar_search"
android:icon="@drawable/ic_search"
android:showAsAction="always|collapseActionView" android:title="Search"
android:actionLayout="@layout/layout_search">
</item>
</menu>
Below is the actionLayout
> layout_search.xml
<?xml version="1.0" encoding="utf-8"?>
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/autoCompText_action_bar_search"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cursorVisible="true"
android:imeOptions="flagNoExtractUi"
android:inputType="text"
android:textColor="@color/color_action_bar_text"
android:textCursorDrawable="@android:color/black"
android:background="@drawable/textfield_bg_activated_holo_dark"
/>
I have also implemented the OnActionExpandListener to listen to the Expand and Collapse Events of the menu Item. Below is my implementation of OnActionExpandListener
private OnActionExpandListener searchActionExpandListener = new OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
/* This is done so that requestFocus() can popup the softkeyboard.
* Else, no softkeyboard is popped up
*/
edtTextSearch.post(new Runnable() {
@Override
public void run() {
edtTextSearch.requestFocus();
mImm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.showSoftInput(edtTextSearch, InputMethodManager.SHOW_IMPLICIT);
}
});
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
mImm.hideSoftInputFromWindow(edtTextSearch.getWindowToken(), 0);
return true;
}
};
So Now, on pressing the search button on Action bar the actionLayout is displayed and the softkeyboard is popped up as well with focus on it. All works fine till now. But when I press the back key (the hard key on the phone), the action view collapses. All i want to do is hide the soft keyboard(if it is being displayed) on pressing the back key and not collapse the action view. Can anyone please help me out?