2

I have an Android sub-menu, declared with this code:

<item
    android:icon="@drawable/ic_menu_share"
    android:showAsAction="ifRoom"
    android:title="Share">
    <menu>
        <item
            android:id="@+id/menuSortNewest"
            android:icon="@drawable/ic_menu_share"/>
        <item
            android:id="@+id/menuSortRating"
            android:icon="@drawable/ic_menu_share"/>
    </menu>
</item>

The problem is that each item in the menu is an icon with a bunch of extra white space to the right of the icon. I just want the icon with no white space beside it. How do I achieve that?

Here is a screenshot of the application:

Ankit Goyal
  • 437
  • 1
  • 7
  • 24
  • You can't just edit the images? Is there any reason you need the white spaces on the right of your icons? – GhostDerfel Mar 11 '14 at 01:44
  • The images do not have any white space beside them. [This](http://i.imgur.com/C9QLgA5.png) is the image I am using for ic_menu_share. – Ankit Goyal Mar 11 '14 at 01:48
  • That's the default width for a context menu item, i've never seen them any thinner. – Broak Mar 11 '14 at 01:48
  • @xBroak you are right, just tested this... What you can do is to build your own menu for this item's, it's kind of reinventing the wheel, but I can't think in any other solution...sorry – GhostDerfel Mar 11 '14 at 01:50
  • @GhostDerfel OK, cool, he can create a custom popupmenu object and inflate his own layout, i'll link to a SO response with a guide on doing it. – Broak Mar 11 '14 at 01:55

1 Answers1

1

OP, As per the discussion in the comments, it's likely you'll need to create your own PopUpMenu to get the functionality you desire, please see:

http://stackoverflow.com/a/21329225/1067946

For info on doing it.

Broak
  • 4,161
  • 4
  • 31
  • 54
  • Thanks @xBroak! Since I am showing this menu when an action bar item is clicked, should my code look like this: `case R.id.action_share: PopupMenu popup = new PopupMenu(context, ); popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu()); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { Toast.makeText(context, "You Clicked: " + item.getTitle(), Toast.LENGTH_SHORT).show(); return true; } }); popup.show();` How should I instantiate the PopupMenu? What arg goes after context? – Ankit Goyal Mar 11 '14 at 02:05
  • The second value is the View it should be attached to, i.e. your action_share button. – Broak Mar 11 '14 at 02:13
  • Ok awesome! Last question, how do I convert my action_share button to a view? I tried `item.getActionView()`, but then I got the error `java.lang.IllegalStateException: MenuPopupHelper cannot be used without an anchor` – Ankit Goyal Mar 11 '14 at 02:14
  • findViewById(R.id.action_share) i should imagine! – Broak Mar 11 '14 at 02:16
  • Nice, that worked! There is one more problem-I added the `android:icon="..."` tag to the item in popup_menu.xml, but the icon doesn't show up. Also, the popup menu is the same size as the original sub-menu. Any thoughts? Thanks so much in advance!!! I've been struggling with this for days and I now I feel like I am getting somewhere. – Ankit Goyal Mar 11 '14 at 02:21
  • Sorry, i'm just as knowledgeable with this as you are! It's 2:27am for me so i cannot continue now, but i urge you to simpy Google! Search for things like 'Android PopUpMenu Custom item layout' and you'll find leads. :) – Broak Mar 11 '14 at 02:28