package de.appetites.android.menuItemSearchAction;
/*
* Copyright (C) 2012 Benjamin Mock
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import vc.rux.englishpoems.free.R;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.widget.SearchViewCompat;
import android.support.v4.widget.SearchViewCompat.OnQueryTextListenerCompat;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnActionExpandListener;
/**
*
* This class creates a MenuItem with an expandable and collapsable ActionView
* for search purposes
*
*
*
* It is based on the great ActionBar Sherlock of Jake Wharton. The problem with
* the original SearchView is, that the style for the dark ActionBar is wrong;
* i.e. black text on a dark gray background.
*
*
* @author Benjamin Mock
*/
public class MenuItemSearchAction extends EditText implements TextWatcher {
private MenuItem searchItem;
private OnQueryTextListenerCompat searchListener;
public MenuItemSearchAction(Context context, MenuItem menuItem,
OnQueryTextListenerCompat listener) {
super(context);
searchListener = listener;
searchItem = menuItem;
createMenuItem();
// show search button on keyboard; this needs the setting of
// TYPE_TEXT... to be shown
setImeOptions(EditorInfo.IME_ACTION_SEARCH);
setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
setMaxLines(1);
setTextColor(Color.WHITE);
setBackgroundColor(Color.BLACK);
}
private MenuItemSearchAction(Context context) {
super(context);
}
public static void bind(Context context, Menu menu, int menuItemId, OnQueryTextListenerCompat listener) {
final MenuItem itemSearch = menu.findItem(R.id.menuSearch);
final View searchView = (View) itemSearch.getActionView();
if (searchView != null) {
SearchViewCompat.setOnQueryTextListener(searchView, listener);
} else {
new MenuItemSearchAction(context, itemSearch, listener);
}
}
/**
* Listen for back button clicks, in order to close the keyboard and
* collapse the ActionView
*/
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_UP) {
searchItem.collapseActionView();
}
return super.dispatchKeyEvent(event);
}
/**
* Returns the the created MenuItem for customizations etc.
*
* @return the MenuItem, which holds search ActionView
*/
public MenuItem getMenuItem() {
return searchItem;
}
private void createMenuItem() {
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
setLayoutParams(layoutParams);
setHint(R.string.menuItemSearchHint);
searchItem.setActionView(this);
searchItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
searchItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// open keyboard
MenuItemSearchAction.this.setText("");
MenuItemSearchAction.this.requestFocus();
MenuItemSearchAction.this.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}, 200);
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// close keyboard
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
MenuItemSearchAction.this.getWindowToken(), 0);
return true;
}
});
setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
searchItem.collapseActionView();
}
return true;
}
});
addTextChangedListener(this);
}
@Override
public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
searchListener.onQueryTextChange(getText().toString());
}
}