11

I am trying to figure out how to show a custom view for items inside the overflow menu (on right of actionbar) but I can't seem to figure it out. It still shows the title only :S. I want to do something like how twitter has it to show an icon, title, and subtitle in the first item in drop down menu. Can anyone help me out

Item in Menu.xml

<item
    android:id="@+id/action_dropdown"
    android:icon="@drawable/ic_action_overflow"
    android:actionProviderClass="efwefwef.com.actionbar.ABDropDown"
    android:title=""
    android:showAsAction="always">
</item>

actionProvider Class #1

public class ABDropDown extends ActionProvider  {
    private static final String TAG = "MActionProvider";
    private Context context;

    public ABDropDown(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public View onCreateActionView() {
        return null;
    }

    @Override
    public boolean hasSubMenu() {
        Log.d(TAG, "hasSubMenu");
        return true;
    }


    @Override
    public boolean onPerformDefaultAction() {
        Log.d(TAG, "onPerformDefaultAction");
        return super.onPerformDefaultAction();
    }

    @Override
    public void onPrepareSubMenu(SubMenu subMenu) {
        subMenu.clear();
        subMenu.add("Profile").setActionProvider(new ABDropDownProfile(context));
        subMenu.add(Menu.NONE, Menu.NONE, 2, "Mezz 2");

    }

}

Second ActionProvider class for the item I want to show a custom view

public class ABDropDownProfile extends ActionProvider  {
    private static final String TAG = "MActionProvider";
    private Context context;

    public ABDropDownProfile(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public View onCreateActionView() {
        View view = View.inflate(context, R.layout.actionbar_viewprofile, null);

        return view;
    }

    @Override
    public boolean hasSubMenu() {
        return false;
    }


    @Override
    public boolean onPerformDefaultAction() {
        Log.d(TAG, "onPerformDefaultAction");
        return super.onPerformDefaultAction();
    }
}
arberb
  • 960
  • 3
  • 14
  • 25
  • 1
    any progress on this issue? – X.X_Mass_Developer Mar 10 '14 at 17:12
  • sadly not :S I ended up not even bothering with it. Possible in the future I may just code a custom dialog view and have it open on button. Of course dynamically calculating the actionbar height so I can position it correctly. If I ever end up coding it I'll post it here – arberb Mar 23 '14 at 20:52
  • How does Google Chrome do it? It shows at the bottom of overflow menu "xx MB saved, since xx xxx". – Mohit Atray Nov 19 '20 at 21:09

2 Answers2

13

I was just facing the same problem on how to list custom rows in overflow menu. Unfortunately there isn't much on web about customising action bars. If you want to list items with just icon and title in over flow menu, you need to do nesting of items within items. I did it like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/ic_action_overflow"
        android:orderInCategory="100"
        android:showAsAction="always">
        <menu>
            <item
                android:id="@+id/add_source"
                android:icon="@drawable/add_on"
                android:orderInCategory="100"
                android:showAsAction="never"
                android:title="@string/add_source"/>
            <item
                android:id="@+id/channel_setup"
                android:icon="@drawable/channelsetup_on"
                android:orderInCategory="100"
                android:showAsAction="never"
                android:title="@string/channel_setup"/>
        </menu>
    </item>

    <item
        android:id="@+id/time"
        android:orderInCategory="99"
        android:showAsAction="always"
        android:title="@string/time_title"/>
    <item
        android:id="@+id/weather"
        android:icon="@drawable/ic_action_cloud"
        android:orderInCategory="98"
        android:showAsAction="always"
        android:title="@string/weather_title"/>
    <item
        android:id="@+id/search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@drawable/ic_action_search"
        android:orderInCategory="97"
        android:showAsAction="collapseActionView|always"
        android:title="@string/search_title"/>

</menu>

to achieve this thing:

enter image description here

And here is the reference to my question:

link

I hope it might help you.

Community
  • 1
  • 1
Shahzeb
  • 3,696
  • 4
  • 28
  • 47
  • Any way of doing this nesting programatically ? – MiaN KhaLiD Sep 29 '14 at 14:05
  • @MiaN KhaLid yes, you can assign custom layout my making your own xml and assigning it to action bar – Shahzeb Sep 30 '14 at 07:13
  • Thats not what i m asking, i do know how to do it using XML or menu files. What i am asking is, is there a way to do this totally through code and no xml ? P.S. i do know how to make menu items and sub menus using code, but i dont hav an idea about nesting menu, like the one in ur solution above using xml. – MiaN KhaLiD Sep 30 '14 at 10:22
  • I don't think there is a way to do this without XML but I now about a library called ' ActionBarSherlock '. You will might find it useful – Shahzeb Sep 30 '14 at 10:43
  • Its the same as Action Bar Compat. I dont think it'll b any different. – MiaN KhaLiD Sep 30 '14 at 10:53
  • To show the icon in the menu, see http://stackoverflow.com/questions/19750635/icon-in-menu-not-showing-in-android – Mr-IDE Jan 20 '17 at 14:44
3

If you just want add a drawable (red dot) after text, you can use imageSpan to add drawable after main text like below:

private MenuItem settingsItem;
    ....
    SpannableStringBuilder builder = new SpannableStringBuilder(getString(R.string.settings_menu_name) + " .");
    Drawable drawable = getResources().getDrawable(R.drawable.red_dot);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    builder.setSpan(new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE), builder.length()-1, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    settingsItem.setTitle(builder);
halfzebra
  • 6,771
  • 4
  • 32
  • 47
Michael
  • 646
  • 6
  • 16
  • 1
    Fantastic! And, in fact, putting a red dot to the right of my Settings menu item was exactly what I wanted to do! – Lawrence Kesteloot Jan 31 '18 at 02:38
  • Thanks, This is exactly what I wanted. I wrapped the drawable in inset drawable and set android:insetBottom to make it appear at the top of TextView. – Mohit Atray Nov 19 '20 at 21:57