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;
}
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;
}
Aggregations