android


Use “SearchView” in Actionbar with ActionbarSherlock


I am trying to "Search" in ActionbarSherlock ver 4.2. ActionbarSherlock has backported SerchView in the latest version.
I have the following code in onCreateOptionsMenu of SherlockListFragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Place an action bar item for searching.
SearchView searchView = new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search Friends");
searchView.setIconified(true);
menu.add(Menu.NONE, Menu.FIRST, Menu.FIRST, "Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, Menu.FIRST + 1, Menu.FIRST + 1, "Search")
.setIcon(R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
and the following code in the
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST:
Toast.makeText(getActivity(),"FIRST", Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST + 1:
Toast.makeText(getActivity(),"FIRST+1", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
I am getting Toast when I click on the "Refresh" button in the ABS but no response when when I click on the "search" it expands and gets converted into EditText but Toast is not fired.
My Question
How to integrate "search" in Actionbar with ABS?
It works.
For implementation of SearchView we need to implement callback interface like this
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
/**
* Called when the user submits the query. This could be due to a key press on the
* keyboard or due to pressing a submit button.
* The listener can override the standard behavior by returning true
* to indicate that it has handled the submit request. Otherwise return false to
* let the SearchView handle the submission by launching any associated intent.
*
* #param newText the query text that is to be submitted
* #return true if the query has been handled by the listener, false to let the
* SearchView perform the default action.
*/
#Override
public boolean onQueryTextSubmit(String newText) {
return true;
}
/**
* Called when the query text is changed by the user.
*
* #param newText the new content of the query text field.
* #return false if the SearchView should perform the default action of showing any
* suggestions if available, true if the action was handled by the listener.
*/
#Override
public boolean onQueryTextChange(String newText) {
ThizLog.d(TAG, "Inside onQueryTextChange");
// called when the action bar search text has changed. Update
// the search filter, and restart the loader to do a new query
// with this filter.
String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
// Don't do anything if the filter hasn't actually changed.
// Prevents restarting the loader when restoring state.
if (mCurFilter == null && newFilter == null) {
return true;
}
if (mCurFilter != null && mCurFilter.equals(newFilter)) {
return true;
}
mCurFilter = newFilter;
getLoaderManager().restartLoader(0, null, this);
return true;
}
});
in this particular case we need to pass the text entered by the user to the CursorLoader and let it reload the cursor with the appropriate results.
This issue was fixed in version 4.3.0 of ActionBarSherlock.

Related Links

Rebooting a device when adb is connected over tcpip
How to add No Results found to Android Database query
jsonArray cannot be converted to JSONObject fetch data from mysql to a list view
What is the efficient way for saving/loading Bitmaps
Error open camera app on Android
Call fragment method inside Activity's oncreate method
Find Push Notification ON
get user information using twitter api on android app
query where relation contains any object from array
How to draw line slowly in Android?
Viewing public calendars in V3?
Android Accessory and Arduino olimexino 32u4
Save checkbox state with sharedpreferences
ScrollView not working with aChartEngine
OnLongClick button listener android
ViewPager in NavigationDrawer failed to load the fragment content when navigation back from another drawer items

Categories

HOME
eclipse
microsoftgraph
twitter-bootstrap-3
gdb
object
graphics
ms-access-2016
static-site
3d-reconstruction
iis-6
vert.x
extjs4.2
spin
java.util.logging
favicon
atlassian
shortest-path
solrcloud
guzzle
babel
exit
scriptella
windows-server-2003
failover
winpe
scatter3d
weather
jett
hard-drive
sharding
python-pptx
google-knowledge-graph
news
magento-1.9.3
parser-generator
movilizer
outputcache
snapchat
jide
hammer.js
openblas
page-replacement
oracleclient
skin
flac
objectscript
javafx-css
gets
visual-studio-2008-sp1
pacemaker
burrows-wheeler-transform
signalr.client
vorpal.js
swizzling
database-relations
toast
crouton-os
wiredep
morton-number
poppler
zen-cart
eoferror
avahi
jquery-forms-plugin
openbabel
filesplitting
sonarqube-5.0
big-ip
contrast
template-deduction
wxformbuilder
paste
cfcache
gd-graph
kendo-dataviz
printdialog
kaleidoscope
asplinkbutton
android-sdk-2.3
nokia-s40
paginator
fitbounds
jquery-address
google-maps-mobile
denied
msr
diagrams
metaweblog
timertask
rowlex
eclipse-marketplace
tabpanel
request-headers
duplex
skype4java
feasibility
wordprocessingml
unmanagedresources
information-management

Resources

Encrypt Message