1

I am have an ActionBar with some menu items that are icons. And what I want is to toggle between two drawables.

This is what I have done so far:

if(id == R.id.shift_day){
if(DoN==0){
item.setIcon(R.drawable.ic_menu_weather_day);
shift="shift night";
DoN=1;
onStart();
}
if(DoN==1){
item.setIcon(R.drawable.ic_menu_weather_night);
shift="shift day";
DoN=0;
onStart();
}
}

This does not work so good because it changes the icon once and never back again.

mrg3tools
  • 29
  • 1
  • 8

2 Answers2

0

You should declare DoN as a static variable and save this DoN variable to SharedPreferences to remember which state we gonna use for later. When you restart the program, get this variable from SharedPreferences and then set icon for menu :)

Community
  • 1
  • 1
xxx
  • 3,315
  • 5
  • 21
  • 40
0

May be you are setting DoN=0 inside onStart() method. Try removing that and instead put return true; Your code if DoN=0 then it first sets DoN=1 then it matches the second block and enters there and set DoN=0 back. Try this:



    if(id == R.id.shift_day){
        if(DoN==0){
            item.setIcon(R.drawable.ic_menu_weather_day);
            shift="shift night";
            DoN=1;
        }
        else if(DoN==1){
            item.setIcon(R.drawable.ic_menu_weather_night);
            shift="shift day";
            DoN=0;
        }
        return true;
    }

Rishav Sengupta
  • 241
  • 2
  • 4
  • This worked really well. Even with the **onStart** thats needed to reaload data from Firebase into a listview depending on the **shift**. – mrg3tools Jul 10 '16 at 16:04