Now I have this search bar up with code below.
Link : http://pastebin.com/6vSBR8iX
How do I let it become the type of search window like the market has?
This is the Android manifest I have.
Link : http://pastebin.com/ue6NSTCr
Someone please help me out with this. I have tried
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
But could not get the search up.
Have you followed the guide here: http://developer.android.com/guide/topics/search/search-dialog.html#PerformingSearch
If you have created Activity and Action Bar Widget, I hope you did not forget to add:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
default:
return false;
}
}
And to clarify your question, could you please post more info?
Cheers!
Related
I'm trying to add functionality to the Search Widget Back Button. Right now, it only closes the search bar. I also want it to close the search list and return the original list before the search.
Search Widget Back Button Image
Attached is my search code.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
winelist = dbHelper.searchWineList(query);
adapter.clear();
adapter.addAll(winelist);
adapter.notifyDataSetChanged();
}
}
there's a listener for SearchView which listens to the closing of search, have you tried having a callback there to reset the whole list?
I have an action bar that is attached to my main activity. There are many different fragments that the activity hosts. In the action bar there are menu items. One is a search view. I have this declared as follows:
menu xml.
<item android:id="#+id/action_search"
android:title="#string/search"
android:icon="#drawable/ic_search_black"
app:showAsAction="always"
app:actionViewClass= "android.support.v7.widget.SearchView"/>
In Main Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
MenuItem item = menu.findItem(R.id.action_search);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
The search view works, when I click the icon the search view expands. I need to capture the click of the search view, as when the user clicks the search view I want to display a view showing the user a list of their recent searches.
I will also be using the search view like so:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, 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
return true;
}
public boolean onQueryTextSubmit(String query) {
//Here u can get the value "query" which is entered in the search box.
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
and carry out the search when the user types 3 characters or more.
I have tried this in the activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case R.id.action_search:
L.i("search", "search");
return false;
case android.R.id.home:
L.i("home", "home pressed");
break;
case R.id.action_done:
L.i("done", "done pressed");
break;
default:
;;
}
return super.onOptionsItemSelected(item);
}
It recognises the done menu item but not the search. I will need to be able to pick up the click in activity and fragment.
So if anyone can point out where I'm going wrong.
Oh, in fragment I've added
setHasOptionsMenu(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case R.id.action_search:
L.i("search", "search");
return false;
case android.R.id.home:
L.i("home", "home pressed");
break;
case R.id.action_done:
//for testing
checkSelectedInterests();
//TODO: MLC
L.i("done", "done pressed");
break;
default:
;;
}
return super.onOptionsItemSelected(item);
}
so if anyone can help I'd appreciate it
So I figured this out. From what I can gather this can't be done via the onOptionsItemSelected method.
So I then set an onclicklistener on the search view which didn't work either. On closer inspection I needed to set and onSearchClickListener as follows:
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
L.i(getClass().getSimpleName(), "SearchViewCLicked");
}
});
Hopefully that'll help someone out!
Here is the simple solution to this, set an onActionExpandListener to the menu item as shown below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
// Get the MenuItem for the action item
MenuItem actionMenuItem = menu.findItem(R.id.action_search);
// Define the listener
actionMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do what you want when it expands
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do what you want when it collapses
return true;
}
});
return true;
}
I have a SearchView in a ActionBar. Now I want to use it but I don't know how to use it. In the custom SearchView I use it like this:
SearchView sear = (SearchView) findViewById(R.id.searchView);
sear.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextSubmit (String query){
return false;
}
#Override
public boolean onQueryTextChange (String newText){
listDataAdapter.getFilter().filter(newText);
return true;
}
}
);
Now I have SearchView in an action bar which I show through code:
public class DataListActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_list_layout);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
android.support.v7.widget.SearchView searchView =
(android.support.v7.widget.SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(getApplicationContext(),MainMenu.class);
startActivity(intent);
return true;
case R.id.action_Exit:
openExit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openExit() {
}
How can I use SearchView ? I don't know how to use SearchView in action bar.
Set the onQueryTextListener(..) similar to what you have mentioned in your question on the searchView.
I'm using this for software search (search that collapses in the menu when the user presses the magnifying glass) and it works perfectly :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_entries, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.viewMenu_search).getActionView();
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.YELLOW);
textView.setHintTextColor(Color.WHITE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return true;
}
#Override
public boolean onQueryTextChange(String s) {
ViewEntries.this.customAdapter.getFilter().filter(s);
return true;
}
});
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
My question is : how do I get the search query string (the one like the parameter String s) when the user starts the search using the hardware button. I tried this, but the intent I get is not the search intent, but the one that started the activity (so I never enter the if):
#Override
public boolean onSearchRequested() {
Intent intent1 = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String s = intent1.getStringExtra(SearchManager.QUERY);
ViewEntries.this.customAdapter.getFilter().filter(s);
}
return super.onSearchRequested();
}
I also say when debugging on the emulator that onSearchRequested is called the moment you start the search, not after you enter the query. Maybe a hint on how to get the searchview there (id or something)?
When a user submits a app wide search in the Search Widget in the ActionBar, I want it to auto collapse after submit. Submit basically opens up the SearchableActivity. Currently, if they close the search results activity (SearchableActivity), the Search Widget is still open. I just want it closed; and with no text.
From reading around, I seem to think that the answer is this
`searchView.onActionViewCollapsed() ;'
however, I am not sure where to put it?
Here is my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setupNewSearchView(searchItem, searchManager);
}
return true;
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupNewSearchView(final MenuItem searchItem,
SearchManager searchManager) {
android.widget.SearchView searchView = (android.widget.SearchView) searchItem
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem About = menu.findItem(R.id.About);
MenuItem Login = menu.findItem(R.id.Login);
MenuItem Logout = menu.findItem(R.id.Logout);
MenuItem Settings = menu.findItem(R.id.Settings);
MenuItem Search = menu.findItem(R.id.menu_search);
MenuItem Add = menu.findItem(R.id.Add);
Add.setVisible(false);
if (LoggedStatus == true) {
// show the log out option
Logout.setVisible(true);
Login.setVisible(false);
Settings.setVisible(true);
} else {
// show the log in option
Logout.setVisible(false);
Login.setVisible(true);
Settings.setVisible(false);
}
About.setVisible(true);
return true;
}
Note, I am using SherlockActionBar.
I did this:
searchView.setOnQueryTextListener(new OnQueryTextListener() {
public boolean onQueryTextChange(String arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean onQueryTextSubmit(String arg0) {
if (searchItem != null) {
searchItem.collapseActionView();
}
return false;
}
});
and in my xml menu file:
android:showAsAction="always|collapseActionView"
it works.