Example usage for android.view SubMenu setHeaderIcon

List of usage examples for android.view SubMenu setHeaderIcon

Introduction

In this page you can find the example usage for android.view SubMenu setHeaderIcon.

Prototype

public SubMenu setHeaderIcon(Drawable icon);

Source Link

Document

Sets the submenu header's icon to the icon given in icon Drawable .

Usage

From source file:com.native5.plugins.ActionBarPlugin.java

private boolean buildMenu(Menu menu, JSONArray definition, String menu_var) {
    // Sadly MenuItem.setIcon and SubMenu.setIcon have conficting return types (for chaining), thus this can't be done w/ generics :(
    class GetMenuItemIconTask extends AsyncTask<String, Void, Drawable> {
        public final MenuItem item;
        public Exception exception = null;

        GetMenuItemIconTask(MenuItem item) {
            this.item = item;
        }//  w ww  .  j  av a 2  s.  com

        @Override
        protected Drawable doInBackground(String... uris) {
            return getDrawableForURI(uris[0]);
        }

        @Override
        protected void onPostExecute(Drawable icon) {
            if (icon != null) {
                item.setIcon(icon);
            }
        }
    }
    ;

    class GetSubMenuIconTask extends AsyncTask<String, Void, Drawable> {
        public final SubMenu item;
        public Exception exception = null;

        GetSubMenuIconTask(SubMenu item) {
            this.item = item;
        }

        @Override
        protected Drawable doInBackground(String... uris) {
            return getDrawableForURI(uris[0]);
        }

        @Override
        protected void onPostExecute(Drawable icon) {
            if (icon != null) {
                item.setIcon(icon);
            }
        }
    }
    ;

    try {
        for (int i = 0; i < definition.length(); ++i) {
            final JSONObject item_def = definition.getJSONObject(i);
            final String text = item_def.isNull("text") ? "" : item_def.getString("text");

            if (!item_def.has("items")) {
                MenuItem item = menu.add(0, i, i, text);
                item.setTitleCondensed(text);
                if (item_def.isNull("icon") == false) {
                    GetMenuItemIconTask task = new GetMenuItemIconTask(item);

                    synchronized (task) {
                        task.execute(item_def.getString("icon"));
                    }
                }

                // Default to MenuItem.SHOW_AS_ACTION_IF_ROOM, otherwise take user defined value.
                item.setShowAsAction(item_def.has("show") ? item_def.getInt("show")
                        : MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

                menu_callbacks.put(item,
                        "var item = " + menu_var + "[" + i + "]; if(item.click) item.click();");
            } else {
                SubMenu submenu = menu.addSubMenu(0, i, i, text);
                if (item_def.isNull("icon") == false) {
                    GetSubMenuIconTask task = new GetSubMenuIconTask(submenu);

                    synchronized (task) {
                        task.execute(item_def.getString("icon"));
                    }
                }

                // Set submenu header
                if (item_def.has("header")) {
                    JSONObject header = item_def.getJSONObject("header");

                    if (header.has("title")) {
                        submenu.setHeaderTitle(header.getString("title"));
                    }

                    if (header.has("icon")) {
                        submenu.setHeaderIcon(getDrawableForURI(header.getString("icon")));
                    }
                }

                // Build sub-menu
                buildMenu(submenu, item_def.getJSONArray("items"), menu_var + "[" + i + "].items");
            }
        }
    } catch (JSONException e) {
        return false;
    }

    return true;
}