Custom Menu on button click as drop down
Asked Answered
K

5

3

I am trying to implement the action bar functionality of the flipkart app.Flipkart App screenshot.

For this I have successfully created a custom Action Bar but I am facing problems in showing the menu as drop down on overflow icon click.

If I try Android Option Menu on button click then on I am getting the menu without icons (4.2.2) at the bottom center of my screen. Using a custom dialog or alert dialog fades the background and they also appear on the center of the screen.

What should I do to get the functionality similar as shown in the image above?

Katharinekatharsis answered 17/9, 2014 at 6:42 Comment(21)
show some codes please.Ury
@IllegalArgument which code you want? I have tried three, the answer link I provided, custom dialog and alert dialog. All of these are working but not providing the exact functionality.Katharinekatharsis
Check : https://mcmap.net/q/265458/-popupmenu-with-icons-duplicateIritis
any code will work. appropriate one would be your menu.xml and your java code. I have implemented this in a number of ways using listmenupopup and xml based too. Need to see what fits your need most.Ury
@IllegalArgument alright, do you have any example to show how this will work with options menu? Currently, when using showOptionMenu, I am getting the menu at the bottom center of the screen and there are no icons (testing on 4.2.2).Katharinekatharsis
this link should work for you #18374683Ury
@IllegalArgument thanks for the link, I'll test it and report back.Katharinekatharsis
@IllegalArgument sadly but Window.FEATURE_ACTION_BAR is not true in my case, as I have a custom ActionBar. Using ActionBar.DISPLAY_SHOW_CUSTOM also isn't working. Any work around for that?Katharinekatharsis
@RohanKandwal post your code I will give it a tryUry
@HareshChhelana Your link is very helpful and completing my needs however setting the offset manually doesn't seem correct. I have tried getting the height of the action bar and using that as Y axis but it is not working properly, you have a work around?Katharinekatharsis
@IllegalArgument The code is too big to be pasted here, since it has navigation drawer and other irrelevant stuff. So should I paste the xml code of custom ActionBar and how I set it up? or you need something more? Anyhow, the code provided by Haresh is working for me but I am having troubles in automating the offset. Can you help in that?Katharinekatharsis
@RohanKandwal,so where is shown popup ?Iritis
@HareshChhelana If I use only the getLocationInWindow() then I am getting the popup on top of button, but as shown in image, I want my popup to be just under actionbar and have some margin from right. Hardcoding works but I want to do it programmatically.Katharinekatharsis
Have you try this showAtLocation() for popupWindow.Iritis
@HareshChhelana yes, I am using the same code as in the link you gave, except the offset. showAsDropDown(view) is also showing the popup under the button but it is getting slightly over actionbar, hence again I need to set offset. I just want to know if there is a way to calculate the offset values programmatically?Katharinekatharsis
No i dnt think so you have use offset as per your requirement and check this : androidresearch.wordpress.com/2012/05/06/….Iritis
@HareshChhelana link not working. So there is no way to get calculate offset programmatically?Katharinekatharsis
@HareshChhelana can you post your comment as an answer? I'll mark it as accepted answer.Katharinekatharsis
@RohanKandwal i am stuck where you were . Can you please help me in resolving pop overactionbar ? I want it under button same as u wanted .Pocketful
@Pocketful get the location of the button, and use showAtLocation() to populate it at the location. You might need to increase/decrease the location to some points, but that will work.Katharinekatharsis
@RohanKandwal ohkay .Got it . And thank you for the reply .Pocketful
I
0

Try this way,hope this will help you to solve your problem.

Please follow link instruction to shown popup menu item with image and you can use showAtLocation() to shown popup as desired location.

Check : PopupMenu with icons

Iritis answered 18/9, 2014 at 4:38 Comment(1)
Thanks, though your comments were very useful, others might see this answer just as a link. Please add some data too.Katharinekatharsis
A
5

You can easily use ListPopupWindow and any convenient for you adapter:

// create any adapter with any data, just as for ordinary ListView
    SimpleAdapter simpleAdapter= new SimpleAdapter(this, dataSet, android.R.layout.simple_list_item_2,
                            new String[] {"Name", "Tel. no."},
                            new int[] {android.R.id.text1, android.R.id.text2});

ListPopupWindow listPopup = new ListPopupWindow(this); // feed context to ListPopupWindow
listPopup.setAdapter(simpleAdapter); // set adapter to created ListPopupMenu

listPopup.setAnchorView(findViewById(id)); 
// id - any id of any view on the screen. Under this view popup appears. It may be MenuItem as well

listPopup.setWidth(getWidestView(simpleAdapter)); 
// as popup width equals the anchor width, use getWidestView method for proper list displaying. 
// ListPopupWindow.WRAP_CONTENT won't work here

listPopup.show();

getWidestView snippet you can find here: https://mcmap.net/q/267115/-wrap_content-for-a-listview-39-s-width

Argyrol answered 2/11, 2015 at 21:23 Comment(0)
C
3

A simple way I have is put the overflow menu by you and add all those menu as sub menu in that, by that way you achieve this view. Also this overflow menu display as always in your ActionBar

<item
    android:icon="@drawable/ic_action_overflow"
    android:showAsAction="always">
    <menu>

        <item
            android:id="@+id/menu_login"
            android:icon="@drawable/login"
            android:title="login"/>
    </menu>
</item>
Carillon answered 17/9, 2014 at 7:19 Comment(2)
simpler and easier :)Meditation
more info please? how to add an item within menu tag??Plumbaginaceous
I
0

Try this way,hope this will help you to solve your problem.

Please follow link instruction to shown popup menu item with image and you can use showAtLocation() to shown popup as desired location.

Check : PopupMenu with icons

Iritis answered 18/9, 2014 at 4:38 Comment(1)
Thanks, though your comments were very useful, others might see this answer just as a link. Please add some data too.Katharinekatharsis
M
0

If you're looking for a more Material popup menu and are not afraid of Kotlin you could try: https://github.com/zawadz88/MaterialPopupMenu - there's no need to declare menus in XML and you could handle clicks more easily.

Miscreant answered 18/7, 2017 at 19:38 Comment(0)
B
-1

The only way that I could get the icons to show was by calling setForceIcons before inflating the menu.

/**
 * Make a call to setForceIcons to cause icons to show.
 *
 * @param popup
 */
public static void showForceIcons (final PopupMenu popup)
{
    try {
        Field[] fields = popup.getClass().getDeclaredFields();
        for (Field field : fields) {
            if ("mPopup".equals(field.getName())) {
                field.setAccessible(true);
                Object menuPopupHelper = field.get(popup);
                Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
                setForceIcons.invoke(menuPopupHelper, true);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Bioclimatology answered 18/4, 2018 at 17:44 Comment(1)
Using reflection is a hacky solution and should be avoided, if you can. The selected answer worked for me, try that once.Katharinekatharsis

© 2022 - 2024 — McMap. All rights reserved.