How to set default option in Android popup menu?

I use the 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 can I set, say, the third default menu option?


EDIT: I want to change the default software changes.

+5
source share
3 answers

Received answers for installing the marked item in xml. If you want to do this in code, use:

popup.getMenu().getItem(2).setChecked(true);

to select the third item. After you inflated the menu, of course ...

+15
source
<item android:id="@+id/jazz"
  android:title="Jazz"
  android:checked="true" />
+4
source

Set android:checked="true"in the item you want to select by default.

+1
source

All Articles