android OptionsMenu checkable item: change without clicking - android

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);

Related

onOptionsSelected is not working

I am trying to hide the menu in the TableLayout with ViewPager I want the menu only in the solutions tab. I used the onPrepareOptionsmenu to hide the menu in tabs except the solutions tab. the thing is my onOptionsItemSelected is not working.
code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.simpleadd,menu);
// +getMenuInflater().inflate(R.menu.simpleadd, menu);
onPrepareOptionsMenu(menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (viewpager.getCurrentItem()==0){
menu.findItem(R.id.simpleadd).setVisible(false);
} else if(viewpager.getCurrentItem()==1){
menu.findItem(R.id.simpleadd).setVisible(false);
} else if(viewpager.getCurrentItem()==2){
menu.findItem(R.id.simpleadd).setVisible(true);
} else if(viewpager.getCurrentItem()==3){
menu.findItem(R.id.simpleadd).setVisible(false);
}else if(viewpager.getCurrentItem()==4){
menu.findItem(R.id.simpleadd).setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.simpleadd:
startActivity(new Intent(this,NewSolution.class));
}
return super.onOptionsItemSelected(item);
}
thanks in advance,
You have used the layout's name in the switch case of onOptionsItemSelected.
Use menu item's id instead.
First, you need to get item id and then set compare with layout ids,
this helps you to set the functionality. Please find the below sample
code for your reference.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.simpleadd) {
// execute your code here
}
return super.onOptionsItemSelected(item);
}

Changing options menu icon

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));

How to switch between two menu icons

I have menu items that are inflated on my toolbar.When one menu item icon is clicked i would like to switch that with another icon and vice versa. I'm doing this so that i can switch between listview and gridview.
What i have tried is to recreate the menu again with a different menu.xml. But the app crashes when i call onCreateOptionsMenu(menu) from onOptionsItemSelected(MenuItem item){}.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_grid) {
listView.setVisibility(View.INVISIBLE);
gv.setVisibility(View.VISIBLE);
//i try to recreate menu again
onCreateOptionsMenu(menu)
}
if (id == R.id.action_list) {
listView.setVisibility(View.VISIBLE);
gv.setVisibility(View.INVISIBLE);
//i try to recreate menu again
onCreateOptionsMenu(menu)
}
return super.onOptionsItemSelected(item);
}
Is there a way to achieve this?
Menu optionsMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.refresh_menu, menu);
optionsMenu = menu;
}
**Somewhere in code**
final MenuItem refreshItem = optionsMenu
.findItem({ID OF MENU Item});
refreshItem.setIcon({New Icon Resource});
You don't need to recreate menu everytime, Just handle MenuItems
#Override
public boolean onOptionsItemSelected(MenuItem item) {
String title = item.getTitle().toString();
switch (title) {
case TITLE_GRIDVIEW:
item.setTitle(TITLE_LISTVIEW);
item.setIcon(R.drawable.listview_icon);
//switch to gridview
return true;
case TITLE_LISTVIEW:
item.setTitle(TITLE_GRIDVIEW);
item.setIcon(R.drawable.gridview_icon);
//switch to listview
return true;
}
return false;
}
It works based on the binded title to your item, In this way you just need one item in your menu to switch between listview and gridview and this items acts like a toggle button.
Instead of trying to re-create the menu, just change the icon and maintain state with a boolean flag:
boolean flag = true;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_grid_list_toggle) {
if (boolean) {
// Show grid view
item.setIcon(R.drawable.ic_grid);
listView.setVisibility(View.INVISIBLE);
gv.setVisibility(View.VISIBLE);
} else {
// Show list view
item.setIcon(R.drawable.ic_list);
listView.setVisibility(View.VISIBLE);
gv.setVisibility(View.INVISIBLE);
}
flag = !flag; // toggle value on every click
}
return super.onOptionsItemSelected(item);
}

Menu is null when activity gets recreated

I have inflated the action bar menu in the base activity and modifying the action bar items in my base activity. But When i change the orientation, menu is always null and thats why it doesn't update my action bar items. Why it null?
Here is my tried code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.base, menu);
mMenu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// switch (item.getItemId()) {
if (item.getItemId() == R.id.action_refresh) {
refreshNewsData();
return true;
}
if (item.getItemId() == R.id.action_share) {
sharenewsData();
return true;
}
if (item.getItemId() == R.id.action_favorite) {
addFavorites();
return true;
}
return super.onOptionsItemSelected(item);
}
I have written this code in base activity.
protected void showIcons(int id) {
if (mMenu != null) {
MenuItem item = mMenu.findItem(id);
item.setVisible(true);
}
}
protected void hideIcons(int id) {
if (mMenu != null) {
MenuItem item = mMenu.findItem(id);
item.setVisible(false);
}
I'm using these methods to update the action bar menu items
You cannot update ActionBar Menu items directly. Whenever you want to update menu items, make a call to invalidateOptionsMenu() this'll redraw the action bar menus and will make a call to onPrepareOptionsMenu(). You'll have to take care of the menu items you want to show / hide inside onPrepareOptionsMenu().

Enable/Disable Options Menu button with .setEnabled()?

Im trying to do something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//multiListener = false;
menu.add(0,START_DELETE,0, "Delete selected..").setEnabled(multiListener);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.multiselect:
if(multiselect == 0) { multiselect = 1;
multiListener = true;
Log.d("DH", "index="+multiListener);
}
else if(multiselect == 1) { multiselect = 0; multiListener = false;
Log.d("DH", "index="+multiListener);
}
fillData();
return true;
case START_DELETE:
Toast.makeText(Notepadv3.this, "Pressed delete", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Basically, if multiListener = true; make "Delete selected.." pressable otherwise gray it out...
This simple , thing.. doesn't want to work out with me,
for somehow... the button is always greyed out, although Log says that, it changes to true...
Anyone, know something?
You should call setEnabled() again to change item's state. Its state doesn't bind to a variable. This method should be called in onPrepareOptionsMenu().

Categories

Resources