I tried to get Search box to work on Action Bar Sherlock.
This is my PreLocationActivity
:
@ContentView(R.layout.search)
public c
A similar question there are here where I had the problem to assign the adapter.
How to implement search widget with a listview using SherlockActionbar?
I hope that it would help because I had the same problem.
To use the adapter the best way is that the class implements SearchView.OnQueryTextListener and then you don´t have to create the inner class and you will have the adapter.
In your code will be something as:
public class MainActivity extends SherlockListActivity implements SearchView.OnQueryTextListener
and then in the class you have to define the methods. The adapter will be the adapter that you have in your class ArrayAdapter adapter. But you should define it as private in the class.
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
adapter.getFilter().filter(newText);
return true;
}
public boolean onQueryTextSubmit(String query) {
// this is your adapter that will be filtered
adapter.getFilter().filter(query);
return true;
}
In the line where you are setting:
searchView.setOnQueryTextListener(queryTextListener);
Should be:
searchView.setOnQueryTextListener(this);
If you have any problem let me know, I was with a similar problem today and read your question without answer.
private EditText search;
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu)
{
menu.add(0, 1, 1, R.string.menu_search).setIcon(R.drawable.ic_action_search).setActionView(R.layout.action_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)
{
switch (item.getItemId())
{
case 1:
search = (EditText) item.getActionView();
search.addTextChangedListener(filterTextWatcher);
search.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
private TextWatcher filterTextWatcher = new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// your search logic here
}
};
Support Library v7: http://developer.android.com/tools/support-library/features.html#v7-appcompat
Google now supports the ActionBar compatibility back to Android 2.1(API 7).
It is easy to make the transition because the method names are the same and/or very similar.
Add the Support Library with Resources: http://developer.android.com/tools/support-library/setup.html#libs-with-res
Your Manifest: AndroidManifest.xml
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
Your Menu: menu.xml
<item
android:id="@+id/menu_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:title="Search"/>
Here is how to use the standard SearchView and SearchManager in Android with ActionBarSherlock! I am using this code and works fine. I have tested this on Android 2.3(API 10) - Android 4.3(API 18).
Great Tutorial and Documentation:
http://developer.samsung.com/android/technical-docs/UI-unification-with-older-Android-versions-using-ActionBarSherlock
Keep in mind:
Custom Search with ActionBarSherlock(min. API 7)
SearchView with ActionBarSherlock(min. API 8)
Your Menu: menu.xml
<item
android:id="@+id/menu_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:title="Search"/>
Your Activity: MainActivity.java
public boolean onCreateOptionsMenu(Menu menu)
{
getSupportMenuInflater().inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
if (null != searchView )
{
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener()
{
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
adapter.getFilter().filter(newText);
return true;
}
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
adapter.getFilter().filter(query);
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
Let me know if this works for you as well and let me know if you need anymore help!