2

I created popup menu for a Image button and applied some styles to it.

How to reduce popup menu width and add a tick mark when the user selects the popup menu item ?

My Code :

btn_a.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            PopupMenu a_popup = new PopupMenu(getActivity().getApplicationContext(), a_period);
            a_popup.getMenuInflater().inflate(R.menu.popup_a, a_popup.getMenu());

            a_popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    Toast.makeText(getActivity().getApplicationContext(), "Clicked "+item.getTitle(), Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
            a_popup.show();
        }
    });
Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53
tenten
  • 1,276
  • 2
  • 26
  • 54

1 Answers1

4

To Add tick mark when popmenu is selected,

Your menu file:

 <?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:checkable="true"
              android:title="Flat" />
        <item android:id="@+id/stadium"
            android:checkable="true"    
              android:title="Stadium" />
        <item android:id="@+id/jazz"
        android:checkable="true"
              android:title="Jazz" />
         <item android:id="@+id/rock"
         android:checkable="true"
              android:title="Rock" />
         <item android:id="@+id/pop"
         android:checkable="true"
              android:title="Pop" />
    </group>
</menu>

in setOnMenuItemClickListener() write below code:

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

It will select 3rd item.

To reduce width of popup menu, you can use Popup Window. Refer this link

Community
  • 1
  • 1
Mehta
  • 1,228
  • 1
  • 9
  • 27