Lets say I have a options menu like below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home_page, menu);
final MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) myActionMenuItem.getActionView();
mSearchView.setQueryHint("Enter text to search(min. 3 chars)...");
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(final String searchQuery) {
if (!mSearchView.isIconified()) {
mSearchView.setIconified(true);
}
mSearchView.setQuery(searchQuery, false);
return false;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
saveState();
finish();
} else if (item.getItemId() == R.id.action_bookmark) {
FragmentAdapter adapter = (FragmentAdapter) mViewPager.getAdapter();
PageFragment fragment = (PageFragment)adapter.instantiateItem(mViewPager,mViewPager.getCurrentItem());
fragment.getContent();
Toast.makeText(this, "Bookmark Added", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
Is there any way to check which icon set to options menu item also change it with another when clicked on it?
here is xml for options menu:
<item
android:id="#+id/action_bookmark"
android:title="Bookmark"
app:showAsAction="always|collapseActionView"
android:icon="#drawable/ic_bookmark_empty" />
onclicking i want to check which icon bookmark has and change it.
if you want to check which drawable is currently set you can use this code
item.getIcon().getConstantState().equals
(getResources().getDrawable(R.drawable.ic_bookmark_empty).getConstantState())
and for setting new drawable you can use this code
item.setIcon(R.drawble.ic_bookmark_empty)
You need to check the following condition in onOptionsItemSelected :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
saveState();
finish();
} else if (item.getItemId() == R.id.action_bookmark) {
if(item.getIcon() ==R.drawable.ic_bookmark_empty)
{
// call bookmark code
item.setIcon(R.drawable.ic_bookmark_filled); // set your desired bookmark icon
} else {
// call remove bookmark code
item.setIcon(R.drawable.ic_bookmark_empty);
}
FragmentAdapter adapter = (FragmentAdapter) mViewPager.getAdapter();
PageFragment fragment = (PageFragment)adapter.instantiateItem(mViewPager,mViewPager.getCurrentItem());
fragment.getContent();
Toast.makeText(this, "Bookmark Added", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
You can use this code menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_bookmark_empty));
Related
I want to open a menu from a OnClickListener
without using the method onCreateOptionsMenu
My code:
toolbar.setNavigationIcon(R.drawable.week); //your icon
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v ) {
}
});
Thanks in advance!
I think you want to show/hide a menu item based on actions from users. To do that you must use onCreateOptionsMenu and whenever you want to show/hide the menu item, then call invalidateOptionsMenu (this method will call onCreateOptionsMenu again).
boolean mShowMenu = false;
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.your_menu, menu);
MenuItem item = menu.findItem(R.id.your_menu_item);
item.setVisible(showMenu);
return true;
}
And in your code, when you want to show menu item.
toolbar.setNavigationIcon(R.drawable.week); //your icon
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v ) {
mShowMenu = true;
invalidateOptionsMenu();
}
});
And give it a try.
You should need to create menu Interface in xml File Like this
<item
android:id="#+id/settings"
android:title="Setting"
app:showAsAction="never" />
<item
android:id="#+id/my_activity"
android:title="My Activity"
app:showAsAction="always"
android:icon="#android:drawable/btn_radio"/>
After that in the Java code of a particular class You need to create the code like this;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.my_activity) {
Intent intent1 = new Intent(this,MyActivity.class);
this.startActivity(intent1);
return true;
}
if (id == R.id.settings) {
Toast.makeText(this, "Setting", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Hopefully this may resolve your problem
I have an options menu which contains one checkable item besides other items.
This is how I set that item checked and store the value inside onOptionsItemSelected.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_uploadCurrList) {
return true;
} else if (id == R.id.action_downloadCurrList) {
return true;
} else if (id == R.id.action_settings) {
return true;
} else if (id == R.id.action_sort_cat) {
item.setChecked(!item.isChecked());
catSort = item.isChecked();
saveSortState(catSort);
return true;
}
return super.onOptionsItemSelected(item);
}
The value of catSort is set inside onCreateOptionsMenu. That's working fine.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_shopping_list, menu);
menu.findItem(R.id.action_sort_cat).setChecked(catSort);
return true;
}
But now, I want to update the isChecked() of that menu item if catSort changed at runtime. I want to do that before the user opens menu. How can I do that? It seems like I can't call setChecked() anywhere else but I have to update that checkbox if my boolean variable changes.
You should use the onPrepareOptionsMenu() callback to update the checked state of your menu item:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_sort_cat);
if (item != null) {
item.setChecked(catSort);
}
return true;
}
Once this is in place, you can update the menu whenever you want by calling
catSort = true; // or false
supportInvalidateOptionsMenu();
Once you have your onPrepareOptionsMenu() callback working, you can even remove this line from onCreateOptionsMenu():
menu.findItem(R.id.action_sort_cat).setChecked(catSort);
How can I handle the click event on the back arrow in the searchview widget:
I tried this code but it doesn't work:
searchtollbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "back arrow clicked");
}
});
also I tried this one:
MenuItemCompat.setOnActionExpandListener(item_search, new
MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true;
}
});
The problem with the above code is that calling onMenuItemActionCollapse() method also executes onQueryTextChange() which is undesirable.
So some help please.
To handle that you have to override onOptionsItemSelected method.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
// handle back event.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try with
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = new SearchView(((MainActivity) mContext).getSupportActionBar().getThemedContext());
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
Log.d( TAG, "expand" );
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
Log.d( TAG, "collapse" );
return true;
}
});
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 have an Activity with DrawerLayout and a RelativeLayout (with ViewPager and a FrameLayout as children for it) and a ListView as children. I am loading a Fragment inside the FrameLayout when an item in the menu inside DrawerLayout is clicked.
The ViewPager loads 3 Fragments. Each of the Fragment has a menu with 3 items. The Fragment that is loaded inside the FrameLayout has menu with 2 items.
The problem is, when i switch between ViewPager and the FrameLayout's Fragment, the menu items are getting added up, to each other.
Please refer the screenshots below..
I am guessing, that i am doing something wrong in onCreateOptionsMenu, onOptionsItemSelected or onPrepareOptionsMenu methods.
Activity's Methods:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerLeft)) {
mDrawerLayout.closeDrawer(mDrawerLeft);
} else {
mDrawerLayout.openDrawer(mDrawerLeft);
}
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
One of the ViewPager's Fragment: Other two are also similar:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_all, menu);
SearchManager searchManager = (SearchManager) activity.
getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.ic_action_search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.
getSearchableInfo(activity.getComponentName()));
searchView.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_list) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.LIST_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(1);
} else if (item.getItemId() == R.id.action_grid) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.GRID_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(2);
} else if (item.getItemId() == R.id.action_mini) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.MINI_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(1);
}
ActivityCompat.invalidateOptionsMenu(activity);
mViewPagerPetitionsRecyclerViewAdapter.notifyDataSetChanged();
mIMainAllPetitionsListener.onLayoutChangedListener();
return super.onOptionsItemSelected(item);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem list_item = menu.findItem(R.id.action_list);
MenuItem grid_item = menu.findItem(R.id.action_grid);
MenuItem mini_item = menu.findItem(R.id.action_mini);
String item_type = prefs.getString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.LIST_VIEW);
if (item_type.equalsIgnoreCase(Const.VIEWPAGER.LIST_VIEW)) {
list_item.setChecked(true);
grid_item.setChecked(false);
mini_item.setChecked(false);
} else if (item_type.equalsIgnoreCase(Const.VIEWPAGER.GRID_VIEW)) {
list_item.setChecked(false);
grid_item.setChecked(true);
mini_item.setChecked(false);
} else if (item_type.equalsIgnoreCase(Const.VIEWPAGER.MINI_VIEW)) {
list_item.setChecked(false);
grid_item.setChecked(false);
mini_item.setChecked(true);
}
}
FrameLayout's Fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_id_card, menu);
MenuItem menuItem = menu.findItem(R.id.edit);
menuItem.setVisible(false);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_share) {
shareIDCard();
} else if (id == R.id.action_save_as_image) {
saveAsImage();
} else if (id == R.id.action_save_as_wallpaper) {
SaveAsWallPaper();
} else if (id == R.id.edit) {
setEnabled(true);
isEditable = true;
ActivityCompat.invalidateOptionsMenu(activity);
} else if (item.getItemId() == R.id.save) {
....
ActivityCompat.invalidateOptionsMenu(activity);
} else if (item.getItemId() == R.id.cancel) {
...
ActivityCompat.invalidateOptionsMenu(activity);
}
return super.onOptionsItemSelected(item);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuItemEdit = menu.findItem(R.id.edit);
MenuItem menuItemSave = menu.findItem(R.id.save);
MenuItem menuItemCancel = menu.findItem(R.id.cancel);
....
menuItemEdit.setVisible(false);
menuItemSave.setVisible(false);
menuItemCancel.setVisible(false);
}
Can someone help me understand why is this happening and what can i do to fix this?
Add menu.clear(); in your onCreateOptionsMenu before inflate