Search in sources :

Example 1 with OnActionExpandListener

use of com.actionbarsherlock.view.MenuItem.OnActionExpandListener in project Android-CollapsibleSearchMenu by johnkil.

the class CollapsibleMenuUtils method addSearchMenuItem.

/**
     * Adding collapsible search menu item to the menu.
     *
     * @param menu
     * @param isLightTheme - true if use light them for ActionBar, else false
     * @return menu item
     */
public static MenuItem addSearchMenuItem(Menu menu, boolean isLightTheme, final OnQueryTextListener onQueryChangeListener) {
    final MenuItem menuItem = menu.add(Menu.NONE, R.id.collapsible_search_menu_item, Menu.NONE, R.string.search_go);
    menuItem.setIcon(isLightTheme ? R.drawable.ic_action_search_holo_light : R.drawable.ic_action_search_holo_dark).setActionView(isLightTheme ? R.layout.search_view_holo_light : R.layout.search_view_holo_dark).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    final View searchView = menuItem.getActionView();
    final AutoCompleteTextView editText = (AutoCompleteTextView) searchView.findViewById(R.id.search_src_text);
    final TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            onQueryChangeListener.onQueryTextChange(charSequence.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    };
    final TextView.OnEditorActionListener onEditorActionListener = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            if (i == EditorInfo.IME_ACTION_SEARCH || keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                onQueryChangeListener.onQueryTextSubmit(textView.getText().toString());
                return true;
            }
            return false;
        }
    };
    menuItem.setOnActionExpandListener(new OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            editText.addTextChangedListener(textWatcher);
            editText.setOnEditorActionListener(onEditorActionListener);
            editText.requestFocus();
            showKeyboard(editText);
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            editText.setText(null);
            editText.removeTextChangedListener(textWatcher);
            // editText.clearFocus();
            hideKeyboard(editText);
            return true;
        }
    });
    final View searchPlate = searchView.findViewById(R.id.search_plate);
    editText.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, final boolean hasFocus) {
            searchView.post(new Runnable() {

                public void run() {
                    searchPlate.getBackground().setState(hasFocus ? new int[] { android.R.attr.state_focused } : new int[] { android.R.attr.state_empty });
                    searchView.invalidate();
                }
            });
        }
    });
    final ImageView closeBtn = (ImageView) menuItem.getActionView().findViewById(R.id.search_close_btn);
    closeBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!TextUtils.isEmpty(editText.getText())) {
                editText.setText(null);
            } else {
                menuItem.collapseActionView();
            }
        }
    });
    return menuItem;
}
Also used : OnFocusChangeListener(android.view.View.OnFocusChangeListener) MenuItem(com.actionbarsherlock.view.MenuItem) ImageView(android.widget.ImageView) AutoCompleteTextView(android.widget.AutoCompleteTextView) TextView(android.widget.TextView) View(android.view.View) KeyEvent(android.view.KeyEvent) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) OnActionExpandListener(com.actionbarsherlock.view.MenuItem.OnActionExpandListener) OnClickListener(android.view.View.OnClickListener) AutoCompleteTextView(android.widget.AutoCompleteTextView) TextView(android.widget.TextView) ImageView(android.widget.ImageView) AutoCompleteTextView(android.widget.AutoCompleteTextView)

Example 2 with OnActionExpandListener

use of com.actionbarsherlock.view.MenuItem.OnActionExpandListener in project Android-CollapsibleSearchMenu by johnkil.

the class CollapsibleMenuUtils method addSearchMenuItem.

/**
	 * Adding collapsible search menu item to the menu.
	 * @param menu
	 * @param isLightTheme - true if use light them for ActionBar, else false
	 * @return
	 */
public static MenuItem addSearchMenuItem(Menu menu, boolean isLightTheme, final TextWatcher textWatcher) {
    final MenuItem menuItem = menu.add(Menu.NONE, R.id.collapsible_search_menu_item, Menu.NONE, R.string.search_go);
    menuItem.setIcon(isLightTheme ? R.drawable.ic_action_search_holo_light : R.drawable.ic_action_search_holo_dark).setActionView(isLightTheme ? R.layout.search_view_holo_light : R.layout.search_view_holo_dark).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    final View searchView = menuItem.getActionView();
    final AutoCompleteTextView editText = (AutoCompleteTextView) searchView.findViewById(R.id.search_src_text);
    menuItem.setOnActionExpandListener(new OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            editText.addTextChangedListener(textWatcher);
            editText.requestFocus();
            showKeyboard(editText);
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            editText.setText(null);
            editText.removeTextChangedListener(textWatcher);
            // editText.clearFocus();
            hideKeyboard(editText);
            return true;
        }
    });
    final View searchPlate = searchView.findViewById(R.id.search_plate);
    editText.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, final boolean hasFocus) {
            searchView.post(new Runnable() {

                public void run() {
                    searchPlate.getBackground().setState(hasFocus ? new int[] { android.R.attr.state_focused } : new int[] { android.R.attr.state_empty });
                    searchView.invalidate();
                }
            });
        }
    });
    final ImageView closeBtn = (ImageView) menuItem.getActionView().findViewById(R.id.search_close_btn);
    closeBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!TextUtils.isEmpty(editText.getText())) {
                editText.setText(null);
            } else {
                menuItem.collapseActionView();
            }
        }
    });
    return menuItem;
}
Also used : OnFocusChangeListener(android.view.View.OnFocusChangeListener) OnActionExpandListener(com.actionbarsherlock.view.MenuItem.OnActionExpandListener) OnClickListener(android.view.View.OnClickListener) MenuItem(com.actionbarsherlock.view.MenuItem) ImageView(android.widget.ImageView) ImageView(android.widget.ImageView) View(android.view.View) AutoCompleteTextView(android.widget.AutoCompleteTextView) AutoCompleteTextView(android.widget.AutoCompleteTextView)

Aggregations

View (android.view.View)2 OnClickListener (android.view.View.OnClickListener)2 OnFocusChangeListener (android.view.View.OnFocusChangeListener)2 AutoCompleteTextView (android.widget.AutoCompleteTextView)2 ImageView (android.widget.ImageView)2 MenuItem (com.actionbarsherlock.view.MenuItem)2 OnActionExpandListener (com.actionbarsherlock.view.MenuItem.OnActionExpandListener)2 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 KeyEvent (android.view.KeyEvent)1 TextView (android.widget.TextView)1