mrg3tools mrg3tools - 4 months ago 15
Android Question

Good practice to do ActionBar icon toggle

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.

Answer

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

Comments