I have a submenu and am trying to get the items to show the currently selected state. As it’s a submenu I can’t call the menu methods. It shows as checked once it has been selected the first time, but I need to make it show when the menu is first inflated. Any ideas please?
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater minf= getMenuInflater();
minf.inflate(R.menu.menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
//-------Options menu----------
case R.id.about:
Intent intentA = new Intent(FuelMoney.this, About.class);
startActivity(intentA);
return true;
case R.id.locale:
return true;
//-----Sub menu---------- UK item not showing as clicked (rest of the code not complete yet)
case R.id.uk_item:
if(this.countryCode.equals("uk"))
{
item.setChecked(true);
}
Toast.makeText(this, "UK selected", Toast.LENGTH_SHORT).show();
this.countryCode="uk";
this.country = new Country(countryCode);
this.regionAttributes();
item.setChecked(true);
return true;
case R.id.us_item:
Toast.makeText(this, "US selected", Toast.LENGTH_SHORT).show();
this.countryCode="us";
this.country = new Country(countryCode);
this.regionAttributes();
return true;
case R.id.eu_item:
Toast.makeText(this, "EU selected", Toast.LENGTH_SHORT).show();
this.countryCode="eu";
this.country = new Country(countryCode);
this.regionAttributes();
return true;
case R.id.jpn_item:
Toast.makeText(this, "Japan selected", Toast.LENGTH_SHORT).show();
this.countryCode="jpn";
this.country = new Country(countryCode);
this.regionAttributes();
return true;
case R.id.india_item:
Toast.makeText(this, "India selected", Toast.LENGTH_SHORT).show();
this.countryCode="ind";
this.country = new Country(countryCode);
this.regionAttributes();
return true;
default :
return super.onOptionsItemSelected(item);
}
Got it. I needed to get the SubMenu and call methods on that.
Here is the complete code for creating an options menu that displays a submenu of choices (in this case countries), which also displays the current setting:
And the xml:
I hope theres enough here so you can fill in the blanks 🙂