Search code examples
androidmaterial-designandroid-alertdialogdialogfragment

How to get such kind of dialog?


this is fab button

this kind of pop up should appear on clicking fab button

I want to have kind of pop shown in the given images. What should use ? Pop window, dialog or alert dialog. i have tried all of these, but not able to match the exact UI.


Solution

  • You can use Popup menu for this purpose.

    enter image description here

    Add this code in your activity.java :

    fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PopupMenu popup = new PopupMenu(HomeActivity.this, fab);
                    popup.getMenuInflater().inflate(R.menu.menu_home, popup.getMenu());
                    popup.setGravity(Gravity.END);
    
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        public boolean onMenuItemClick(MenuItem item) {
    
                            switch (item.getItemId()) {
                                case R.id.action_settings:
                                    // your action
                                default:
                                    return true;
                            }
                        }
                    });
    
                    popup.show();
                }
            });
    

    In style.xml

      <style name="MyPopupMenu" parent="@style/Widget.AppCompat.PopupMenu.Overflow">
        <item name="fontPath">Muli_Regular.ttf</item>
        <item name="android:dropDownHorizontalOffset">0dp</item>
        <item name="android:dropDownVerticalOffset">57dp</item>
    </style>
    

    use this style in activity theme as :

    <item name="popupMenuStyle">@style/MyPopupMenu</item>