How to switch between two menu icons - android

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

Related

android OptionsMenu checkable item: change without clicking

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

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

onprepareoptionsmenu was called by default

I got a need to change the menu items dynamically during a click event of menu items.
So I implemented onOptionsItemSelected in an activity.
public class ResultActivity extends 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.activity_main_actions, menu);
return true;
}
}
and i'm calling invalidateOptionsMenu in the onOptionsItemSelected method which inturn should call the onPrepareOptionsMenu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_group: {
invalidateOptionsMenu();
break;
}
}
}
Also, I am trying to remove one item from menu in onPrepareOptionsMenu method.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
Log.d(TAG,"In onPrepareOptionsMenu");
menu.removeItem(R.id.action_group);
Log.d(TAG,"Group icon is removed");
return super.onPrepareOptionsMenu(menu);
}
The problem is, item is deleted during the launch of activity rather than wait until corresponding menu item is clicked.
Can someone tell what the problem is.. TIA
The problem is that onPrepareOptionsMenu(Menu) gets called anyway, anytime your menu needs to be shown or reloaded. That includes calls coming from invalidateOptionsMenu(), but also from the Activity being created.
You could, for example, check for a boolean state before actually removing the item.
public boolean mRemoveItem;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_group: {
mRemoveItem = true;
invalidateOptionsMenu();
break;
}
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (mRemoveItem) {
menu.removeItem(R.id.action_group);
}
return super.onPrepareOptionsMenu(menu);
}
According to your needs, you will need to set mRemoveItem back to false at some point in your code.

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().

How to get menu item id on action bar when other menu item clicked

So I have menu items on action bar. on onOptionsItemSelected, I want to change the menu items images.
Here's my code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.todaySched:{
viewTodaySched();
item.setIcon(R.drawable.calendarselected);
infoLog=(MenuItem)findViewById(R.id.infoLog);
infoLog.setIcon(R.drawable.book);
return true;}
case R.id.infoLog:{
viewInfoLog();
item.setIcon(R.drawable.bookselected);
todaySched=(MenuItem)findViewById(R.id.todaySched);
todaySched.setIcon(R.drawable.calenderselected);
return true;}
default:
return super.onOptionsItemSelected(item);
}
}
But the icon won't change when I clicked it, and I got run time error.
e.g: When I click todaySched icon, It seems like I can't get the infoLog item id.
My LogCat: LogCat
As per you logcat, you getting class cast exception and you have used sharlockactionbar.
so try and check if you have imported the correct MenuItem and Menu which should like this:
import com.actionbarsherlock.view.MenuItem;
and
import com.actionbarsherlock.view.Menu;
instead of
import android.view.MenuItem;
and
import android.view.Menu;
Edit:
Here is how you can change both icons on just a single click:
private Menu menu;
private MenuItem item1, item2;
Boolean original = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
this.menu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.todaySched) {
update();
} else if (id == R.id.infoLog) {
update();
}
return true;
}
private void update() {
item1 = menu.findItem(R.id.todaySched);
item2 = menu.findItem(R.id.infoLog);
if (original) {
item1.setIcon(getResources().getDrawable(
android.R.drawable.ic_menu_search));
item2.setIcon(getResources().getDrawable(
android.R.drawable.ic_menu_report_image));
original = false;
} else if (!original) {
item1.setIcon(getResources().getDrawable(
android.R.drawable.ic_menu_my_calendar));
item2.setIcon(getResources().getDrawable(
android.R.drawable.ic_menu_info_details));
original = true;
}
}
checked and is working. Now use it as per your requirement..
Cheers....
Every time you want to make changes to your items in the Action bar you have to call the function
invalidateOptionsMenu(). Then you override the method public boolean onPrepareOptionsMenu(Menu menu), there you get your menu items and you can set icons, create new actions or remove them. Hope it helps.

Categories

Resources