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;
}
Related
My Action bar items were working fine until I added a menu drawer, now when the activity is first displayed, the switch button on the menu doesn't work and as soon as the drawer opens it starts working absolutely fine. Although I have not called it in my onDrawerOpened method.
// ----------For Options Menu-------------------
#Override
public boolean onCreateOptionsMenu(Menu menu) {
try {
super.onCreateOptionsMenu(menu);
menu.clear();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_screen_menu, menu);
menuitem1 = menu.findItem(R.id.menu_item1);
menuitem2 = menu.findItem(R.id.menu_item2);
final Switch getView = (Switch) menuitem2.getActionView();
getView.setChecked(false);
getView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0,
boolean isSelected) {
if (isSelected) {
method1();
} else {
method2();
}
}
});
} catch (Exception e) {
e.printStackTrace();
Log.e("OnCreateOptionsMenu", "exception", e);
}
mOptionsMenu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.menu_item1: {
return true;
}
case R.id.menu_item2: {
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
Once I open and close the Drawer than the switch is working perfectly!
Can anyone please help?
Thanks in advance.
You're supposed to call super.onCreateOptionsMenu(menu) after having inflated the menu (as shown in the docs : http://developer.android.com/guide/topics/ui/actionbar.html)
I would change your code to :
// ----------For Options Menu-------------------
#Override
public boolean onCreateOptionsMenu(Menu menu) {
try {
menu.clear();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_screen_menu, menu);
(...)
return super.onCreateOptionsMenu(menu);
}
(Not totally sure that it could fix your issue, but it's worth the try)
I did a search widget, which works fine. But when I pressed the back button, onCancel and onDismiss are never call.
In the PCRListActivity.java :
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
#Override
public void onCancel() {
getLoaderManager().restartLoader(LOADER_ID_PRODUCTS, null, PCRListActivity.this);
}
});
searchManager.setOnDismissListener(new SearchManager.OnDismissListener() {
#Override
public void onDismiss() {
getLoaderManager().restartLoader(LOADER_ID_PRODUCTS, null, PCRListActivity.this);
}
});
return true;
}
So, what am I doing wrong ?
I also meet this problem, and I have no choice but give up "oncloselistener". Instead, you can get your menuItem, then setOnActionExpandListener. Then override unimplents methods.
I guess that close and dissmiss methods just work for Progress Dialogs.
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//do what you want to when close the sesarchview
//remember to return true;
return true;
}
When controlling a MenuItem, I have been doing this:
Menu menu;
(...)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
this.menu = menu;
return true;
}
(...)
public void handleSearch(View view) {
Button button = (Button) view;
if(menu.findItem(R.id.action_search).isVisible()) {
button.setText(R.string.button_search_show);
menu.findItem(R.id.action_search).setVisible(false);
} else {
button.setText(R.string.button_search_hide);
menu.findItem(R.id.action_search).setVisible(true);
}
}
"this" being referencing the menu created in onCreateOptionsMenu with a Menu that any method in the class can use. The handleSearch method controls MenuItems by using findItem twice. This doesn't feel very conventional or efficient (a very scientific observation, I might add). Is there a more conventional or efficient way to do this?
You can save MenuItem in a variable instead of using findItem twice.
MenuItem myMenuitem = menu.findItem(R.id.action_search);
There is a method that handles actionBar's menu itself.
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return (true);
case R.id.menu_home:
Intent i = new Intent(this, HomeActivity.class);
startActivity(i);
return (true);
}
return (super.onOptionsItemSelected(item));
}
I am displaying menu action bar at the bottom of the screen. when user click/touch any of the menu item, i want to highlight it (i.e. the way button click highlighting happens). i tried onClickListener and ontouchListener but it doesn't highlight.
can someone tell me which porperty/method i have set.
Here is code i am using.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.landing_page_layout);
ActionBar actionBar = getActionBar();
actionBar.show();
// business logic }
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_items, menu);
item1 = menu.findItem(R.id.menu_option1);
item1.getActionView().setOnTouchListener(new OnTouchListener() {
// logic when user touch menu option1 touch
}});
Thanks
Chintan
Check this section in the documentation: http://developer.android.com/guide/topics/ui/menus.html#options-menu
To set the menu up you do this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
Where R.menu.menu points to your res/menu/menu.xml file. This will load the elements from that file
The options menu is listened to in the same way as regular View's with OnClickListeners and such. Instead you onOptionsItemSelected override like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.item1:
// Do something
return true;
case R.id.item2:
// Do something else
return true;
default:
return super.onOptionsItemSelected(item);
}
}
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!