Instantly share code, notes, and snippets.
Created
August 28, 2012 16:56
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save ruXlab/3500582 to your computer and use it in GitHub Desktop.
Alternative SearchView for ActionBar. Read more about http://rux.pp.ru/2012.08/android-actionbar-search-view/ (in russian)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
/** | |
* <p> | |
* This class creates a MenuItem with an expandable and collapsable ActionView | |
* for search purposes | |
* </p> | |
* | |
* <p> | |
* 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. | |
* </p> | |
* | |
* @author Benjamin Mock <[email protected]> | |
*/ | |
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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment