how can I capture the back arrow command in Android SearchView.
need to hide the previously opened list
You can use setOnActionExpandListener of MenuItem
mSearchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
//TODO Here you can do your list hiding stuff :D
return true;
}
});
I've the base activity where all toolbar initializations and options menu are done, the activities extending the base can not fire onitemclick
In the base i have
public class BaseActivity extends AppCompatActivity {
private MenuItem refresh;
public Toolbar getToolbar() {
return toolbar;
}
public MenuItem getRefresh() {
return refresh;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
refresh = menu.findItem(R.id.action_refresh);
refresh.setActionView(R.layout.menu_item_view);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return false;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
switch (mState) {
case Const.STATE_IDLE:
refresh.setVisible(true);
break;
case STATE_WORKING:
refresh.setVisible(false);
break;
default:
refresh.setVisible(true);
break;
}
return super.onPrepareOptionsMenu(menu);
}
}
In one of the activites I handle it like
public class CommentsActivity extends BaseToolbarActivity
{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId() == R.id.action_refresh){
setState(Const.STATE_WORKING);
showMsg(contentRoot,"oops");
return true;
}
return super.onOptionsItemSelected(item);
}
}
but the click items won't fire
After a while I came to realize that since I was setting a custom layout for my items (useful for animations) an option menu with a custom view that is either set in the xml or dynamically using
item.setActionView(R.layout.menu_lay);
Just like my problem above the menu item can never be invoked by the normal onOptionsItemSelected listener so the way to make it work is to implement onClickListener on the item's custom View so in my case the way to make it invoke is by
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getRefresh.getActionView().setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//refresh some data
}
});
return true;
}
I just tried this code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.action_search:
MenuItemCompat.setOnActionExpandListener(item.findItem(R.id.action_search),
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
System.out.println("expand");
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
System.out.println("collapse");
return true;
}
});
return true;
default:
return super.onOptionsItemSelected(item);
}
}
But it shows me an error, saying :
Cannot resolve method "findItem(int)"
I checked on android developer and the method seems to be valid the way I use it.
Does anyone know why it doesn't work ?
Thanks
Docs says it should be called Menu
item.findItem(R.id.action_search) // Here item is MenuItem not Menu
Try this way
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.action_search:
MenuItemCompat.setOnActionExpandListener(item,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
System.out.println("expand");
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
System.out.println("collapse");
return true;
}
});
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I am adding a menu item for texts selection. normally it shows cut,copy,share, etc.. I added a one more item to this menu and named the item as "Mark".
For this I added the following code in my Activity.Java
#Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
menu.add("Mark");
mode.getMenuInflater().inflate(R.menu.main, menu);
}
super.onActionModeStarted(mode);
}
I am able to get my menu item while long press on texts.. Below is the screen, which reflects my menu item.
For this menu item, I want to do something while select it. So, I used the following code.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if( item.getTitle().equals("Mark")){
System.out.println("MenuItem Mark clicked----");
Log.d("MenuItem clicked----", "Mark");
}
return super.onOptionsItemSelected(item);
}
Here, I am not able to get the message "MenuItem Mark clicked----" or "MenuItem clicked---- Mark" in my Logcat.
How may I do this?
update
Followed Elitz's answer, but still no luck. my Changes below
#Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
menu.add(0,1000,0,"Mark");
mode.getMenuInflater().inflate(R.menu.main, menu);
}
super.onActionModeStarted(mode);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 1000) {
System.out.println("MenuItem Mark clicked----");
Log.d("MenuItem clicked----", "Mark");
}
return super.onOptionsItemSelected(item);
}
Added updated answer and still no messages came in Logcat
private ActionMode.Callback startActionMode = (new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu yourMenu) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu yourMenu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem yourMenu) {
Log.d("MenuItem clicked----303", "Mark");
if (yourMenu.getItemId() == R.attr.actionModeSelectAllDrawable) {
System.out.println("MenuItem Mark clicked----305");
Log.d("MenuItem clicked----", "Mark");
}
return true;
}
});
update2
Followed Ankesh's answer also,
#Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
menu.add(R.id.privateText);
mode.getMenuInflater().inflate(R.menu.main, menu);
}
super.onActionModeStarted(mode);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.privateText) {
System.out.println("MenuItem Mark clicked----");
Log.d("MenuItem clicked----", "Mark");
}
return super.onOptionsItemSelected(item);
}
Both attempt there is no logs available...
Update3
For test the other menu, I found id for selectAll menu item in R,java, and tried the following code,
#Override
public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.attr.actionModeSelectAllDrawable) {
System.out.println("MenuItem All clicked----");
Log.d("MenuItem clicked----", "All");
}
return super.onOptionsItemSelected(item);
}
this is also not showing the message in Logcat. Is this function is right one for menu items selection. or what else I am missing in this?
use this instead of your menu.add("Mark");
add(int groupId, int itemId, int order, CharSequence title);
now you have your Id now you can check for that.
groupId = 0; and order = 0;, or any number that fits your choice, but since in your example you have only 1 group, just put 0.
hmm.. i think you got us all fooled :) yes it will not work, because you are using ActionMode right and with ActionMode you need to specify a callback for it, so you should put your code here
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem arg1) {
return false;
}
in its callback. like something like this, when you call startActoinMode
startActionMode(new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu yourMenu) {
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu yourMenu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem yourMenu) {
//put your item click here
return false;
}
});
Use
if (item.getItemId() == R.id.xyz) {
}
Can you just add your menu item inside your xml menu(I mean inside R.menu.main)? And after that just detect the item click inside this method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_id:
//TODO click detected!!
break
default:
return super.onOptionsItemSelected(item);
}
}
Its that or I don't understand your question :)
I want to handle collapsing state but I can not handle onActionExpandListener. I use actionbarsherlock and here are my codes. Where do I make mistake?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
menu.getItem(0).setVisible(aramaOnOff);
menu.getItem(1).setVisible(filterOnOff);
SearchView searchView = (SearchView)menu.findItem(R.id.imgBuyutec).getActionView();
searchView.setOnQueryTextListener(this);
searchViewItem = menu.findItem(R.id.imgBuyutec);
searchViewItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
return true;
}
Use appcompat-v7 , http://developer.android.com/guide/topics/ui/actionbar.html
and then use the following code,
MenuItemCompat.setOnActionExpandListener(searchViewItem,new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
//whatever
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//whatever
return true;
}
});