Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Popup Menus

I want to use popup menu for two(2) buttons. The way I am achieving it right now is by making two separate xml files popup_menu1.xml and popup_menu2.xml which are inflated for each button, button1 and button2 respectively. Popup-menu-1 has two(2) menu items and popup-menu-2 has four(4) menu items. The pics are added for more clarification.

popup_menu1.xml has two menu-items, a seperate xml-file popup_menu2.xml has four menu-items, a separate xml-file

each file is inflated for individual buttons.

My Question is: Can I use(inflate) only one(1) xml-file, instead of two separate xml-files, for two(2) buttons and showing two(2) different popup_menus?

Any help is appreciated.

Thank you,

P.S: I wanted to add pictures for more clarity, but as a new member they are not allowing me to do it.

Code Added: popup_menu1: File1

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu001">
  <group android:id="@+id/group_popupmenu">
      <item android:id="@+id/menu1"
          android:title="Today's Date"/>
      <item android:id="@+id/menu2"
          android:title="Custom Date"/>
  </group>
</menu>

popup_menu2: File2

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu002">
  <group android:id="@+id/group_popupmenu">
      <item android:id="@+id/menu1"
          android:title="Last Seven (07) Days"/>
      <item android:id="@+id/menu2"
          android:title="Today"/>
      <item android:id="@+id/menu003"
          android:title="Yesterday"/>
      <item android:id="@+id/menu4"
          android:title="Last Twenty Eight (28) Days"/>
  </group>
</menu>

Edit version 1: Code In Activity Class:

// add a click listener to the first button
startDateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
    PopupMenu popup = new PopupMenu (CampaignDetailsActivity.this, view);
    popup.getMenuInflater().inflate(R.layout.popup_menu_01, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    public boolean onMenuItemClick(MenuItem item) {

        switch(item.getItemId()){
    case R.id.menu1:
    // some code here
    case R.id.menu2:
    // some code here   
        }
    return true;
    });
 popup.show();
 }
});

// add a click listener to the end date button
endDateButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View button) {
    PopupMenu popup = new PopupMenu (TestAdlikelyButtonsAndMenuActivity.this, button);
            popup.getMenuInflater().inflate(R.layout.popup_menu2, popup.getMenu());

            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    switch(item.getItemId()){
                    case R.id.menu1:
                    // some code here
                    case R.id.menu2:
                    // some code here
                    case R.id.menu003:
                    // some code here
                    case R.id.menu4:
                    // some code here
                    }
                    return true;
                }
            });
        popup.show();
    }
});

Images:

enter image description here

enter image description here

like image 375
Ahmad Bilal Avatar asked May 23 '26 04:05

Ahmad Bilal


1 Answers

Yes, you can inflate a single XML file multiple times, but in the example you gave, you're doing it right by having two XML files. After inflating, but before calling PopupMenu.show(), you can use one of the variants of Menu.add() to add items to the Menu (the one returned by PopupMenu.getMenu()).

like image 192
Darshan Rivka Whittle Avatar answered May 26 '26 11:05

Darshan Rivka Whittle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!