Activity menu icons

83 views
Skip to first unread message

David Given

unread,
Nov 2, 2008, 9:41:38 AM11/2/08
to android-d...@googlegroups.com
My application extensively uses menus constructed from activities
declared in the manifest, using Menu.addIntentOptions() to get the
system to automatically merge in any appropriate menu items based on the
current activities' URL. This is working very well.

Unfortunately, I haven't figured out how to make icons appear for the
menu items. The label is correctly taken from android:label on the
activity's <intent-filter>, or the activity's own label if one isn't
specified, but android:icon appears to be completely ignored.

Hunting for info, I found the following mailing list thread:

http://groups.google.com/group/android-developers/browse_thread/thread/5ca8bc78fdaa9e69

...of people running into the same problem with m5. Unfortunately, there
wasn't a resolution.

Has anyone got this working? If so, how?

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────

│ ⍎'⎕',∊N⍴⊂S←'←⎕←(3=T)⋎M⋏2=T←⊃+/(V⌽"⊂M),(V⊝"M),(V,⌽V)⌽"(V,V←1⎺1)⊝"⊂M)'
│ --- Conway's Game Of Life, in one line of APL

signature.asc

Jason Parekh

unread,
Nov 2, 2008, 1:28:43 PM11/2/08
to android-d...@googlegroups.com
Hey David,

Looks like there's no way in 1.0 to have addIntentOptions set the icon on each created menu item.  I just fixed this, so in the next release it will work.

As an interim solution, you could, in your onCreateOptionsMenu, go through each menu item and do:

Intent intent = item.getIntent();
if (intent != null) item.setIcon(packageManager.getActivityIcon(intent));

jason

David Given

unread,
Nov 2, 2008, 2:30:06 PM11/2/08
to android-d...@googlegroups.com
Jason Parekh wrote:
[...]

> As an interim solution, you could, in your onCreateOptionsMenu, go
> through each menu item and do:
>
> Intent intent = item.getIntent();
> if (intent != null) item.setIcon(packageManager.getActivityIcon(intent));

Thanks, that ought to do it --- but I can't seem to find any way of
iterating through a Menu and looking at each MenuItem! The only way I
can see of getting a MenuItem out of a Menu is to call Menu.findItem();
but that needs an id, and they're all likely to be 0. What am I missing?

signature.asc

Jason Parekh

unread,
Nov 2, 2008, 11:36:48 PM11/2/08
to android-d...@googlegroups.com
doh, you're correct, there isn't a way for clients to iterate through the items in the menu. :(

at this point, I suggest switching to hard-coding the menu items.  if you really want to populate the menu via intents, you may have to copy the (fixed) addIntentOptions code into a helper class:

    public int addIntentOptions(int group, int id, int categoryOrder, ComponentName caller,
            Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems) {
        PackageManager pm = mContext.getPackageManager();
        final List<ResolveInfo> lri =
                pm.queryIntentActivityOptions(caller, specifics, intent, 0);
        final int N = lri != null ? lri.size() : 0;

        if ((flags & FLAG_APPEND_TO_GROUP) == 0) {
            removeGroup(group);
        }

        for (int i=0; i<N; i++) {
            final ResolveInfo ri = lri.get(i);
            Intent rintent = new Intent(
                ri.specificIndex < 0 ? intent : specifics[ri.specificIndex]);
            rintent.setComponent(new ComponentName(
                    ri.activityInfo.applicationInfo.packageName,
                    ri.activityInfo.name));
            final MenuItem item = add(group, id, categoryOrder, ri.loadLabel(pm))
                    .setIcon(ri.loadIcon(pm))
                    .setIntent(rintent);
            if (outSpecificItems != null && ri.specificIndex >= 0) {
                outSpecificItems[ri.specificIndex] = item;
            }
        }

        return N;

Peli

unread,
Nov 10, 2008, 3:44:41 PM11/10/08
to Android Developers
Great! Thanks a lot! This finally solves the problem I had a long time
ago ( http://groups.google.com/group/android-developers/browse_frm/thread/5ca8bc78fdaa9e69
) :-)

Peli
Reply all
Reply to author
Forward
0 new messages