I am using following code to create a menu:
PopupMenu popup = new PopupMenu(getApplicationContext(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.equalizer, popup.getMenu());
popup.show();
equalizer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="@+id/flat"
android:title="Flat" />
<item android:id="@+id/stadium"
android:title="Stadium" />
<item android:id="@+id/jazz"
android:title="Jazz" />
<item android:id="@+id/rock"
android:title="Rock" />
<item android:id="@+id/pop"
android:title="Pop" />
</group>
</menu>
How is it possible to set, let's say, 3rd option of the menu as selected by default?
EDIT: I would like to change defaults programmatically.
Answers have been given for setting a checked item in xml. If you want to do it in code, use:
popup.getMenu().getItem(2).setChecked(true);
to select the 3rd item. After you inflated the menu of course...
Set android:checked="true"
in item which you want select default.
<item android:id="@+id/jazz"
android:title="Jazz"
android:checked="true" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With