0

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?

Viren
  • 105
  • 2
  • 10
  • Hi Viren, you should also take a look at my answer for a similar question: [Contextual ActionBar hides when I click on hardware backbutton and the keyboard is out](http://stackoverflow.com/a/23122941/2668136), it might help you. – Blo Apr 19 '14 at 14:28
  • Thanks Filo for such an active response. Your link gave me a fresh perspective – Viren Apr 19 '14 at 19:30
  • Glad to help! It's weird because I never saw this behaviour on my projects. I'll do some test to know what it's wrong/right in mine. Anyway, I hope this will help you. Good coding. – Blo Apr 19 '14 at 19:37

1 Answers1

0

I had a similar problem with the ActionBarSherlock and a CollapseActionView. Your code helped me a lot but I was unable to pop the Soft Keyboard on earlier versions (as 2.+) with a Runnable() (don't know why). Just a little trick may avoid you to use this way, see my code below:

@Override
public boolean onMenuItemActionExpand(MenuItem item) {              
    // Set the focus
    edtTextSearch.requestFocus();  
    // This force the Soft Keyboard to appear whatever the version used
    mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED,  
                                   InputMethodManager.HIDE_IMPLICIT_ONLY);
    return true;
}  

This works perferctly in 2.+ and above. Now, for what you ask, I don't see that things on all devices tested. When the View expands, the Soft Keyboard appears. Then when I press the back button, the Soft Keyboard dissapears and the View doesn't collapse (even in landscape mode). If I'm press it again, here the View collapse.
Can you explain more what you want because I don't see properly your action on back button to collapse the View?

After some researches, I was thinking that you wanted to keep your DownDropList visible with the CollapseActionView, so maybe it will be usefull to look this: DropDownList of AutoCompleteTextView opened after pressing the Back key?. And just in case, I found this: EditText with soft keyboard and “Back” button

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99