Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Show DropDown Menu on MenuItem click

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!

like image 206
Faizan Mubasher Avatar asked Apr 20 '15 08:04

Faizan Mubasher


3 Answers

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);
     }
 }
like image 115
Hoan Nguyen Avatar answered Oct 06 '22 00:10

Hoan Nguyen


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  
         }  
like image 35
Jignesh Jain Avatar answered Oct 05 '22 23:10

Jignesh Jain


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/

like image 26
Charaf Eddine Mechalikh Avatar answered Oct 06 '22 01:10

Charaf Eddine Mechalikh