Android Show DropDown Menu on MenuItem click

17,259

Solution 1

Create your menu xml as follow

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
       android:id="@+id/menu_item_action_parameters"
       android:title="@string/text_parameters"
       android:icon="@drawable/ic_menu_parameter"
       app:showAsAction="ifRoom|withText"/> >
       <menu>
          <item 
            android:id="@+id/action_dropdown1"
            android:title="@string/dropdown_1" />
          <item 
            android:id="@+id/action_dropdown2"
            android:title="@string/dropdown2" />
          <item 
            android:id="@+id/action_dropdown3"
            android:title="@string/dropdown3" />
        </menu>
    </item>

    <item
      more item
    </item>
</menu>

Then

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_dropdown1:
            ...
            return true;

        case R.id.action_dropdown2:
            ...
            return true;
        ...

        default:
            return super.onOptionsItemSelected(item);
     }
 }

Solution 2

what about showing popup menu when clicking onthat item ? here is the code :

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();



        if (id == R.id.action_notifi) {
        // here we show the popup menu
        popup();
        }

        return super.onOptionsItemSelected(item);
    }


    public void popup(){
          PopupMenu popup = new PopupMenu(MainActivity.context, v); //the v is the view that you click replace it with your menuitem like : menu.getItem(1)
                    popup.getMenuInflater().inflate(R.menu.medecin_list_menu,
                            popup.getMenu());
                    popup.show();
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item2) {

                            switch (item2.getItemId()) {
                                case R.id.Appeler:
                                 //do somehting

                                    break;
                                case R.id.EnvoyerMsg:
                                 //do somehting

                                    break;
                                case R.id.AfficherDet:
                            //do somehting

                                    break;
                                case R.id.Afficher: 
                        //do somehting
                                    break;
                                case R.id.AvoirRdv:
                              //do somehting
                                    break;

                                default:
                                    break;
                            }

                            return true;
                        }
                    });

                }
            });
    }

and here is the medecin_list_menu (my menu)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/Appeler"
        android:title="@string/Appeler" />
    <item
        android:id="@+id/EnvoyerMsg"
        android:title="@string/envoyerMsg" />
    <item
        android:id="@+id/Afficher"
        android:title="@string/Afficher" />
    <item
        android:id="@+id/AvoirRdv"
        android:title="@string/avoirRdv" />
    <item
        android:id="@+id/AfficherDet"
        android:title="@string/afficherDet" />

</menu>

Last Edit: see this tutorial http://www.androidhive.info/2013/11/android-working-with-action-bar/

Solution 3

try custom popup menu

menu.Xml

<menu xmlns:androclass="http://schemas.android.com/apk/res/android" >  

    <item  
        android:id="@+id/one"  
        android:title="One"/>  

    <item  
        android:id="@+id/two"  
        android:title="Two"/>  

    <item  
        android:id="@+id/three"  
        android:title="Three"/>  

</menu>  

call this code on buttonClick

 button = (Button) findViewById(R.id.button1);  
          button1.setOnClickListener(new OnClickListener() {  

           @Override  
           public void onClick(View v) {  
            //Creating the instance of PopupMenu  
            PopupMenu popup = new PopupMenu(MainActivity.this, button1);  
            //Inflating the Popup using xml file  
            popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu());  

            //registering popup with OnMenuItemClickListener  
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
             public boolean onMenuItemClick(MenuItem item) {  
              Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  
              return true;  
             }  
            });  

            popup.show();//showing popup menu  
           }  
          });//closing the setOnClickListener method  
         }  
Share:
17,259

Related videos on Youtube

Faizan Mubasher
Author by

Faizan Mubasher

Senior Software Engineer. .NET Core Android React Native ReactJS

Updated on July 27, 2022

Comments

  • Faizan Mubasher
    Faizan Mubasher almost 2 years

    I want to show DropDown menu on MenuItem click just like this.

    enter image description here

    Like this

    enter image description here

    Note that this item was added like:

    <item
        android:id="@+id/menu_item_action_parameters"
        android:title="@string/text_parameters"
        android:icon="@drawable/ic_menu_parameter"
        app:showAsAction="ifRoom|withText"/>
    </item>
    

    And in my code:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.menu_item_action_parameters:
                // What to do here?
                break;
        }
        return super.onOptionsItemSelected(item);
     }
    

    I have seen this link but I have came to know that ActionBar.setListNavigationCallbacks() has been deprecated.

    Thanks!

  • Faizan Mubasher
    Faizan Mubasher about 9 years
    See the updated question! I already know the thing you have mentioned.
  • Hoan Nguyen
    Hoan Nguyen about 9 years
    That looks like a pop up menu.
  • Charaf Eddine Mechalikh
    Charaf Eddine Mechalikh about 9 years
    there is a way but it is not the most simple ...i've seen a tutorial that may help .i will google it and tell you if i find it
  • Charaf Eddine Mechalikh
    Charaf Eddine Mechalikh about 9 years
    see the tutorial i posted
  • Faizan Mubasher
    Faizan Mubasher about 9 years
    Thanks! Yop! Its working the way I want. I just totally forgot that we can also create sub menus. Thanks anyway.
  • Admin
    Admin over 4 years
    I looked at the tutorial and dont see where it mentions this. It would have been good if you showed the declaration of v; cause I am clueless what v is. Is it a menuItem? Cause a menuItem is what the popup should lock to.