Question about overflow menu

0 giorgi sheklashvili · September 25, 2015
item.setChecked(false) is not working in my app. It should uncheck radiobutton if its checked and I click it right?I wrote whole app just like it's in bucky's 33th tutorial.


public boolean onOptionsItemSelected(MenuItem item) {
RelativeLayout main_view=(RelativeLayout)findViewById(R.id.main_view);
switch (item.getItemId()){
case R.id.menu_red:
if(item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
main_view.setBackgroundColor(Color.RED);
return true;
case R.id.menu_green:
if(item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
main_view.setBackgroundColor(Color.GREEN);
return true;
case R.id.menu_yellow:
if(item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
main_view.setBackgroundColor(Color.YELLOW);
return true;
default:
return super.onOptionsItemSelected(item);

}
}

Post a Reply

Replies

Oldest  Newest  Rating
+1 Gary Whitney · September 26, 2015
I think it is working but not the way you think it should work.

Run the app in debug mode and set a break point on the switch. (I use run continue when I get to the end of the switch because I don't want to step into all the behind the scenes stuff.)
Try it both when you haven't clicked it and when you have already clicked it. the code works but the GUI doesn't change  the way you  think  it should. The state that you are setting and un-setting is not the state of the view that you see on the GUI.

Actually this is pretty common behavior.for radio groups. I have seen this in MS Access as well.
  • 1

Java / Android Development

107,252 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator