Example usage for android.view MenuItem getIcon

List of usage examples for android.view MenuItem getIcon

Introduction

In this page you can find the example usage for android.view MenuItem getIcon.

Prototype

public Drawable getIcon();

Source Link

Document

Returns the icon for this item as a Drawable (getting it from resources if it hasn't been loaded before).

Usage

From source file:org.brandroid.openmanager.activities.OpenExplorer.java

private ImageButton makeMenuButton(final MenuItem item, ViewGroup parent) {
    ImageButton btn = (ImageButton) getLayoutInflater().inflate(R.layout.toolbar_button, parent, false);
    if (!item.isVisible())
        btn.setVisibility(View.GONE);
    Drawable d = item.getIcon();
    if (d instanceof BitmapDrawable)
        ((BitmapDrawable) d).setGravity(Gravity.CENTER);
    btn.setImageDrawable(d);/*w  w w.  java  2s.c o  m*/
    btn.setId(item.getItemId());
    btn.setOnClickListener(this);
    btn.setLongClickable(true);
    btn.setFocusable(true);
    btn.setOnFocusChangeListener(this);
    btn.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            showToast(item.getTitle());
            return true;
        }
    });
    return btn;
}

From source file:org.mozilla.gecko.BrowserApp.java

/**
 * Add the provided item to the provided menu, which should be
 * the root (mMenu)./* www  .j  a va 2s .c  om*/
 */
private void addAddonMenuItemToMenu(final Menu menu, final MenuItemInfo info) {
    info.added = true;

    final Menu destination;
    if (info.parent == 0) {
        destination = menu;
    } else if (info.parent == GECKO_TOOLS_MENU) {
        // The tools menu only exists in our -v11 resources.
        if (Versions.feature11Plus) {
            final MenuItem tools = menu.findItem(R.id.tools);
            destination = tools != null ? tools.getSubMenu() : menu;
        } else {
            destination = menu;
        }
    } else {
        final MenuItem parent = menu.findItem(info.parent);
        if (parent == null) {
            return;
        }

        Menu parentMenu = findParentMenu(menu, parent);

        if (!parent.hasSubMenu()) {
            parentMenu.removeItem(parent.getItemId());
            destination = parentMenu.addSubMenu(Menu.NONE, parent.getItemId(), Menu.NONE, parent.getTitle());
            if (parent.getIcon() != null) {
                ((SubMenu) destination).getItem().setIcon(parent.getIcon());
            }
        } else {
            destination = parent.getSubMenu();
        }
    }

    final MenuItem item = destination.add(Menu.NONE, info.id, Menu.NONE, info.label);

    item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Menu:Clicked",
                    Integer.toString(info.id - ADDON_MENU_OFFSET)));
            return true;
        }
    });

    if (info.icon == null) {
        item.setIcon(R.drawable.ic_menu_addons_filler);
    } else {
        final int id = info.id;
        BitmapUtils.getDrawable(this, info.icon, new BitmapUtils.BitmapLoader() {
            @Override
            public void onBitmapFound(Drawable d) {
                // TODO: why do we re-find the item?
                final MenuItem item = destination.findItem(id);
                if (item == null) {
                    return;
                }
                if (d == null) {
                    item.setIcon(R.drawable.ic_menu_addons_filler);
                    return;
                }
                item.setIcon(d);
            }
        });
    }

    item.setCheckable(info.checkable);
    item.setChecked(info.checked);
    item.setEnabled(info.enabled);
    item.setVisible(info.visible);
}

From source file:com.zoffcc.applications.zanavi.Navit.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    ///*from w  w w .  ja v a  2 s  . com*/
    menu.clear();

    // load the menu from XML
    getMenuInflater().inflate(R.menu.actionbaricons, menu);

    // NavitOverflowMenuItemID = R.id.item_overflow_menu_button;
    menu.findItem(R.id.share_menu_destination).setTitle(Navit.get_text("Share Destination"));
    menu.findItem(R.id.share_menu_location).setTitle(Navit.get_text("Share my Location"));
    menu.findItem(R.id.search_menu_offline).setTitle(get_text("address search (offline)"));
    menu.findItem(R.id.search_menu_online).setTitle(get_text("address search (online)"));
    menu.findItem(R.id.item_recentdest_menu_button).setTitle(get_text("Recent destinations"));
    menu.findItem(R.id.item_settings_menu_button).setTitle(get_text("Settings"));
    menu.findItem(R.id.item_search_menu_button).setTitle(get_text("Search"));

    // --- download icon ---
    menu.findItem(R.id.item_download_menu_button).setTitle(get_text("downloading map"));
    // --- download icon ---

    //
    menu.findItem(R.id.overflow_share_location).setTitle(Navit.get_text("Share my Location"));
    menu.findItem(R.id.overflow_share_destination).setTitle(Navit.get_text("Share Destination"));
    menu.findItem(R.id.overflow_settings).setTitle(Navit.get_text("Settings"));
    menu.findItem(R.id.overflow_zoom_to_route).setTitle(Navit.get_text("Zoom to Route"));

    if (ZANaviNormalDonateActivity.on_amazon_device) {
        menu.findItem(R.id.overflow_donate_item).setTitle(Navit.get_text("Donate"));
    } else {
        menu.findItem(R.id.overflow_donate_item).setTitle(Navit.get_text("Donate with Google Play"));
    }
    menu.findItem(R.id.overflow_donate_bitcoins_item).setTitle(Navit.get_text("Donate with Bitcoin"));
    //. TRANSLATORS: text to translate is: exit ZANavi
    menu.findItem(R.id.overflow_exit).setTitle(Navit.get_text("exit navit"));
    menu.findItem(R.id.overflow_toggle_poi).setTitle(Navit.get_text("toggle POI"));
    menu.findItem(R.id.overflow_announcer_on).setTitle(Navit.get_text("Announcer On"));
    menu.findItem(R.id.overflow_announcer_off).setTitle(Navit.get_text("Announcer Off"));
    menu.findItem(R.id.overflow_download_maps).setTitle(Navit.get_text("download maps"));
    menu.findItem(R.id.overflow_delete_maps).setTitle(Navit.get_text("delete maps"));
    menu.findItem(R.id.overflow_maps_age).setTitle(Navit.get_text("show Maps age"));
    menu.findItem(R.id.overflow_coord_dialog).setTitle(Navit.get_text("Coord Dialog"));
    menu.findItem(R.id.overflow_add_traffic_block).setTitle(Navit.get_text("add Traffic block"));
    menu.findItem(R.id.overflow_clear_traffic_block).setTitle(Navit.get_text("clear Traffic blocks"));
    menu.findItem(R.id.overflow_convert_gpx_file).setTitle(Navit.get_text("convert GPX file"));
    menu.findItem(R.id.overflow_replay_gps_file).setTitle(Navit.get_text("replay a ZANavi gps file"));
    menu.findItem(R.id.overflow_yaml_tests).setTitle(Navit.get_text("run YAML tests"));
    menu.findItem(R.id.overflow_clear_gpx_map).setTitle(Navit.get_text("clear GPX map"));
    // menu.findItem(R.id.overflow_dummy2)
    menu.findItem(R.id.overflow_demo_v_normal).setTitle(get_text("Demo Vehicle") + " [normal]");
    menu.findItem(R.id.overflow_demo_v_fast).setTitle(get_text("Demo Vehicle") + " [fast]");
    menu.findItem(R.id.overflow_speech_texts).setTitle(Navit.get_text("Speech Texts"));
    menu.findItem(R.id.overflow_nav_commands).setTitle(Navit.get_text("Nav. Commands"));
    menu.findItem(R.id.overflow_toggle_route_graph).setTitle(Navit.get_text("toggle Routegraph"));
    //menu.findItem(R.id.overflow_dummy1)
    menu.findItem(R.id.overflow_export_map_points_to_sdcard).setTitle(Navit.get_text("export Destinations"));
    menu.findItem(R.id.overflow_import_map_points_from_sdcard).setTitle(Navit.get_text("import Destinations"));
    menu.findItem(R.id.overflow_send_feedback).setTitle(Navit.get_text("send feedback"));
    menu.findItem(R.id.overflow_online_help).setTitle(Navit.get_text("online Help"));
    menu.findItem(R.id.overflow_about).setTitle(Navit.get_text("About"));
    //. TRANSLATORS: it means: "show current target in google maps"
    //. TRANSLATORS: please keep this text short, to fit in the android menu!
    menu.findItem(R.id.overflow_target_in_gmaps).setTitle(Navit.get_text("Target in gmaps"));
    //
    //
    menu.findItem(R.id.item_share_menu_button).setTitle(get_text("Share"));

    Display display_ = getWindowManager().getDefaultDisplay();
    Log.e("Navit", "Navit width in DP -> " + display_.getWidth() / Navit.metrics.density);
    Log.e("Navit", "Navit width in DP -> density=" + Navit.metrics.density);

    try {
        View v4 = findViewById(R.id.item_settings_menu_button);
        // Log.e("Navit", "Navit width in DP -> v4=" + v4);
        if ((v4 != null) && (v4.getWidth() > 0)) {
            Log.e("Navit", "Navit width in DP -> v4.w=" + v4.getWidth());
            MenuItem menuItem = menu.findItem(R.id.item_settings_menu_button);
            // Log.e("Navit", "Navit width in DP -> mi=" + menuItem);
            // Log.e("Navit", "Navit width in DP -> i=" + menuItem.getIcon());
            Log.e("Navit", "Navit width in DP -> i.w=" + menuItem.getIcon().getIntrinsicWidth());
            actionbar_item_width = (int) ((v4.getWidth() + (menuItem.getIcon().getIntrinsicWidth() * 1.5f))
                    / 2);
        } else {
            MenuItem menuItem = menu.findItem(R.id.item_settings_menu_button);
            // Log.e("Navit", "Navit width in DP -> mi=" + menuItem);
            // Log.e("Navit", "Navit width in DP -> i=" + menuItem.getIcon());
            Log.e("Navit", "Navit width in DP -> i.w=" + menuItem.getIcon().getIntrinsicWidth());
            actionbar_item_width = (int) ((menuItem.getIcon().getIntrinsicWidth()) * 1.7f);
        }

        actionbar_items_will_fit = display_.getWidth() / actionbar_item_width;
        Log.e("Navit", "Navit width in DP -> number of items that will fit=" + actionbar_items_will_fit);
        if (actionbar_items_will_fit > 6) // now we need to fit max. 6 items on actionbar
        {
            actionbar_all_items_will_fit = true;
        } else {
            actionbar_all_items_will_fit = false;
        }
    } catch (Exception e) {
        if ((display_.getWidth() / Navit.metrics.density) < NAVIT_MIN_HORIZONTAL_DP_FOR_ACTIONBAR) {
            actionbar_all_items_will_fit = false;
        } else {
            actionbar_all_items_will_fit = true;
        }
    }

    if (actionbar_all_items_will_fit == false) {
        menu.findItem(R.id.item_share_menu_button).setVisible(false);
        menu.findItem(R.id.overflow_share_location).setVisible(true);
        if (NavitGraphics.CallbackDestinationValid2() == 0) {
            menu.findItem(R.id.overflow_share_destination).setVisible(false);
        } else {
            menu.findItem(R.id.overflow_share_destination).setVisible(true);
        }

        if (actionbar_items_will_fit < 6) {
            // also push the settings icons to overflow menu
            menu.findItem(R.id.item_settings_menu_button).setVisible(false);
            menu.findItem(R.id.overflow_settings).setVisible(true);
        } else {
            menu.findItem(R.id.item_settings_menu_button).setVisible(true);
            menu.findItem(R.id.overflow_settings).setVisible(false);
        }
    } else {
        menu.findItem(R.id.item_settings_menu_button).setVisible(true);
        menu.findItem(R.id.overflow_settings).setVisible(false);
        menu.findItem(R.id.overflow_share_location).setVisible(false);
        menu.findItem(R.id.overflow_share_destination).setVisible(false);
        menu.findItem(R.id.item_share_menu_button).setVisible(true);
    }

    cur_menu = menu;

    System.out.println("down_icon:000");

    try {
        MenuItem downloadViewMenuItem = menu.findItem(R.id.item_download_menu_button);
        System.out.println("down_icon:001");
        downloadViewMenuItem.setVisible(true);
        System.out.println("down_icon:002");
        System.out.println("down_icon:003");
        android.widget.ImageView v = (android.widget.ImageView) MenuItemCompat
                .getActionView(menu.findItem(R.id.item_download_menu_button));
        System.out.println("down_icon:004");
        v.setVisibility(View.VISIBLE);
        if (v != null) {
            System.out.println("down_icon:005");
            v.setImageBitmap(null);
            System.out.println("down_icon:006");

            v.setBackgroundResource(R.drawable.anim_download_icon_2);
            final AnimationDrawable anim = (AnimationDrawable) v.getBackground();

            // ----------------------------------
            // thanks to: http://stackoverflow.com/questions/14686802/animationdrawable-is-not-working-on-2-3-6-android-version
            // ----------------------------------
            v.post(new Runnable() {
                public void run() {
                    try {
                        anim.start();
                        System.out.println("down_icon:006a");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            System.out.println("down_icon:007");

            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent mapdownload_cancel_activity = new Intent(Navit.this,
                            ZANaviDownloadMapCancelActivity.class);
                    mapdownload_cancel_activity
                            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(mapdownload_cancel_activity);
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("down_icon:099:EE");
    }

    if (actionabar_download_icon_visible) {
        menu.findItem(R.id.item_download_menu_button).setVisible(true);
    } else {
        menu.findItem(R.id.item_download_menu_button).setVisible(false);
    }

    if (NavitGraphics.CallbackDestinationValid2() > 0) {
        menu.findItem(R.id.item_endnavigation_menu_button).setVisible(true);
        menu.findItem(R.id.item_endnavigation_menu_button).setTitle(get_text("Stop Navigation"));
        menu.findItem(R.id.overflow_zoom_to_route).setVisible(true);
        menu.findItem(R.id.overflow_target_in_gmaps).setVisible(true);
        menu.findItem(R.id.share_menu_destination).setVisible(true);
    } else {
        menu.findItem(R.id.item_endnavigation_menu_button).setVisible(false);
        menu.findItem(R.id.overflow_zoom_to_route).setVisible(false);
        menu.findItem(R.id.overflow_target_in_gmaps).setVisible(false);
        menu.findItem(R.id.share_menu_destination).setVisible(false);
    }

    if (Navit_Announcer == true) {
        menu.findItem(R.id.overflow_announcer_off).setVisible(true);
        menu.findItem(R.id.overflow_announcer_on).setVisible(false);
    } else {
        menu.findItem(R.id.overflow_announcer_off).setVisible(false);
        menu.findItem(R.id.overflow_announcer_on).setVisible(true);
    }

    if (p.PREF_enable_debug_functions) {
        menu.findItem(R.id.overflow_dummy2).setVisible(true);
        menu.findItem(R.id.overflow_demo_v_normal).setVisible(true);
        menu.findItem(R.id.overflow_demo_v_fast).setVisible(true);
        menu.findItem(R.id.overflow_speech_texts).setVisible(true);
        menu.findItem(R.id.overflow_nav_commands).setVisible(true);
        menu.findItem(R.id.overflow_toggle_route_graph).setVisible(true);
        menu.findItem(R.id.overflow_replay_gps_file).setVisible(true);
        menu.findItem(R.id.overflow_yaml_tests).setVisible(true);
    } else {
        menu.findItem(R.id.overflow_dummy2).setVisible(false);
        menu.findItem(R.id.overflow_demo_v_normal).setVisible(false);
        menu.findItem(R.id.overflow_demo_v_fast).setVisible(false);
        menu.findItem(R.id.overflow_speech_texts).setVisible(false);
        menu.findItem(R.id.overflow_nav_commands).setVisible(false);
        menu.findItem(R.id.overflow_toggle_route_graph).setVisible(false);
        menu.findItem(R.id.overflow_replay_gps_file).setVisible(false);
        menu.findItem(R.id.overflow_yaml_tests).setVisible(false);
    }

    return true;
}