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?

Community
  • 1
  • 1
Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
  • show some codes please. – Illegal Argument Sep 17 '14 at 06:45
  • @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. – Rohan Kandwal Sep 17 '14 at 06:46
  • 2
    Check : http://stackoverflow.com/questions/15454995/popupmenu-with-icons – Haresh Chhelana Sep 17 '14 at 06:47
  • 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. – Illegal Argument Sep 17 '14 at 06:48
  • @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). – Rohan Kandwal Sep 17 '14 at 06:56
  • this link should work for you http://stackoverflow.com/questions/18374183/how-to-show-icons-in-overflow-menu-in-actionbar – Illegal Argument Sep 17 '14 at 06:57
  • @IllegalArgument thanks for the link, I'll test it and report back. – Rohan Kandwal Sep 17 '14 at 07:00
  • @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? – Rohan Kandwal Sep 17 '14 at 07:26
  • @RohanKandwal post your code I will give it a try – Illegal Argument Sep 17 '14 at 07:34
  • @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? – Rohan Kandwal Sep 17 '14 at 08:07
  • @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? – Rohan Kandwal Sep 17 '14 at 08:10
  • @RohanKandwal,so where is shown popup ? – Haresh Chhelana Sep 17 '14 at 08:35
  • @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. – Rohan Kandwal Sep 17 '14 at 09:33
  • Have you try this showAtLocation() for popupWindow. – Haresh Chhelana Sep 17 '14 at 09:39
  • @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? – Rohan Kandwal Sep 17 '14 at 09:43
  • No i dnt think so you have use offset as per your requirement and check this : http://androidresearch.wordpress.com/2012/05/06/how-to-create-popups-in-android/. – Haresh Chhelana Sep 17 '14 at 09:46
  • @HareshChhelana link not working. So there is no way to get calculate offset programmatically? – Rohan Kandwal Sep 17 '14 at 10:26
  • @HareshChhelana can you post your comment as an answer? I'll mark it as accepted answer. – Rohan Kandwal Sep 18 '14 at 04:31
  • @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 . – young_08 May 26 '16 at 08:45
  • @young_08 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. – Rohan Kandwal May 26 '16 at 11:32
  • @RohanKandwal ohkay .Got it . And thank you for the reply . – young_08 May 26 '16 at 11:44

5 Answers5

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://stackoverflow.com/a/13959716/4890659

Community
  • 1
  • 1
Anton
  • 449
  • 2
  • 7
  • 20
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>
Pratik
  • 30,639
  • 18
  • 84
  • 159
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

Community
  • 1
  • 1
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • 1
    Thanks, though your comments were very useful, others might see this answer just as a link. Please add some data too. – Rohan Kandwal Sep 18 '14 at 05:30
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.

Piotr Zawadzki
  • 1,678
  • 1
  • 18
  • 24
-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();
    }
}
pstorli
  • 127
  • 1
  • 6
  • Using reflection is a hacky solution and should be avoided, if you can. The selected answer worked for me, try that once. – Rohan Kandwal Apr 19 '18 at 08:12