Example usage for android.view MenuItem setIcon

List of usage examples for android.view MenuItem setIcon

Introduction

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

Prototype

public MenuItem setIcon(@DrawableRes int iconRes);

Source Link

Document

Change the icon associated with this item.

Usage

From source file:org.opendatakit.survey.android.activities.MainMenuActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    int showOption = MenuItem.SHOW_AS_ACTION_IF_ROOM;
    MenuItem item;
    if (currentFragment != ScreenList.WEBKIT) {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_USE_LOGO | ActionBar.DISPLAY_SHOW_TITLE);
        actionBar.show();//from  w w  w .jav  a 2s  . c o m

        item = menu.add(Menu.NONE, MENU_FILL_FORM, Menu.NONE, getString(R.string.enter_data_button));
        item.setIcon(R.drawable.ic_action_collections_collection).setShowAsAction(showOption);

        // Using a file for this work now
        String get = PropertiesSingleton.getProperty(appName, AdminPreferencesActivity.KEY_GET_BLANK);
        if (get.equalsIgnoreCase("true")) {
            item = menu.add(Menu.NONE, MENU_PULL_FORMS, Menu.NONE, getString(R.string.get_forms));
            item.setIcon(R.drawable.ic_action_av_download).setShowAsAction(showOption);

            item = menu.add(Menu.NONE, MENU_CLOUD_FORMS, Menu.NONE, getString(R.string.get_forms));
            item.setIcon(R.drawable.ic_action_cloud).setShowAsAction(showOption);
        }

        String send = PropertiesSingleton.getProperty(appName, AdminPreferencesActivity.KEY_SEND_FINALIZED);
        if (send.equalsIgnoreCase("true")) {
            item = menu.add(Menu.NONE, MENU_PUSH_FORMS, Menu.NONE, getString(R.string.send_data));
            item.setIcon(R.drawable.ic_action_av_upload).setShowAsAction(showOption);
        }

        String manage = PropertiesSingleton.getProperty(appName, AdminPreferencesActivity.KEY_MANAGE_FORMS);
        if (manage.equalsIgnoreCase("true")) {
            item = menu.add(Menu.NONE, MENU_MANAGE_FORMS, Menu.NONE, getString(R.string.manage_files));
            item.setIcon(R.drawable.trash).setShowAsAction(showOption);
        }

        String settings = PropertiesSingleton.getProperty(appName,
                AdminPreferencesActivity.KEY_ACCESS_SETTINGS);
        if (settings.equalsIgnoreCase("true")) {
            item = menu.add(Menu.NONE, MENU_PREFERENCES, Menu.NONE, getString(R.string.general_preferences));
            item.setIcon(R.drawable.ic_menu_preferences).setShowAsAction(showOption);
        }
        item = menu.add(Menu.NONE, MENU_ADMIN_PREFERENCES, Menu.NONE, getString(R.string.admin_preferences));
        item.setIcon(R.drawable.ic_action_device_access_accounts).setShowAsAction(showOption);

        item = menu.add(Menu.NONE, MENU_ABOUT, Menu.NONE, getString(R.string.about));
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
    } else {
        ActionBar actionBar = getActionBar();
        actionBar.hide();
    }

    return true;
}

From source file:com.github.akinaru.hcidebugger.activity.HciDebuggerActivity.java

private void stopScan() {

    if (mScanning) {
        runOnUiThread(new Runnable() {
            @Override/*from  w w  w.j a va 2s . c  om*/
            public void run() {

                if (mScanType == ScanType.CLASSIC_SCAN) {
                    mBluetoothAdapter.cancelDiscovery();
                } else {
                    Toast.makeText(HciDebuggerActivity.this, getResources().getString(R.string.toast_scan_stop),
                            Toast.LENGTH_SHORT).show();
                    mBluetoothAdapter.stopLeScan(scanCallback);
                }

                mScanning = false;

                MenuItem item;
                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    item = nvDrawer.getMenu().findItem(R.id.scan_btn_nv);
                } else {
                    item = toolbar.getMenu().findItem(R.id.scan_btn);
                }

                if (item != null) {
                    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                        item.setIcon(R.drawable.ic_looks);
                    } else {
                        item.setIcon(R.drawable.ic_action_scanning);
                    }
                    item.setTitle(getResources().getString(R.string.menu_item_title_start_scan));
                }
                //Toast.makeText(HciDebuggerActivity.this, getResources().getString(R.string.toast_scan_stop), Toast.LENGTH_SHORT).show();
            }
        });
    } else {
        Log.v(TAG, "not scanning");
    }
}

From source file:com.github.akinaru.hcidebugger.activity.HciDebuggerActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    this.getMenuInflater().inflate(R.menu.toolbar_menu, menu);

    //clear button
    MenuItem item = menu.findItem(R.id.clear_btn);
    if (item != null) {
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override//from  w  ww.  j  ava  2  s . c o  m
            public boolean onMenuItemClick(MenuItem item) {
                clearAdapter();
                return true;
            }
        });
    }

    //bluetoooth scan button
    item = menu.findItem(R.id.scan_btn);
    if (item != null) {
        item.setIcon(R.drawable.ic_action_scanning);
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                toggleScan(item);
                return true;
            }
        });
    }

    //bluetooth state button
    item = menu.findItem(R.id.state_bt_btn);
    if (item != null) {
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                toggleBtState();
                return true;
            }
        });
        if (mBluetoothAdapter.isEnabled()) {
            item.setIcon(R.drawable.ic_bluetooth);
        } else {
            item.setIcon(R.drawable.ic_bluetooth_disabled);
        }
    }

    //reset snoop file button
    if (Build.VERSION.SDK_INT <= 22) {
        item = menu.findItem(R.id.reset_snoop_file);
        item.setVisible(true);
        if (item != null) {
            item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    resetSnoopFile();
                    refresh();
                    return true;
                }
            });
        }
    }

    //filter button
    item = menu.findItem(R.id.filter_btn);
    if (item != null) {
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                filter();
                return true;
            }
        });
    }

    //refresh button
    item = menu.findItem(R.id.refresh);
    if (item != null) {
        if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_PORTRAIT) {

            item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    refresh();
                    return true;
                }
            });
        } else {
            item.setVisible(false);
        }
    }

    item = menu.findItem(R.id.share);

    if (item != null) {
        // Fetch and store ShareActionProvider
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
        setSharedIntent();
    }

    return super.onCreateOptionsMenu(menu);
}

From source file:saphion.fragments.alarm.AlarmFragment.java

/**
 * Installs click and touch listeners on a fake overflow menu button.
 * //from  w w  w.jav a 2 s.c  o m
 * @param menuButton
 *            the fragment's fake overflow menu button
 */
public void setupFakeOverflowMenuButton(View menuButton) {
    final PopupMenu fakeOverflow = new PopupMenu(menuButton.getContext(), menuButton) {
        @Override
        public void show() {
            getActivity().onPrepareOptionsMenu(getMenu());
            super.show();
        }
    };
    Menu menu = fakeOverflow.getMenu();// .inflate(R.menu.bmenu);
    MenuItem noti = menu.add("Notification Settings");
    noti.setIcon(R.drawable.noti);
    noti.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            getActivity().startActivity(new Intent(getBaseContext(), Controller.class));
            getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
            return true;
        }
    });

    MenuItem prefs = menu.add("Preferences");
    prefs.setIcon(R.drawable.prefs);
    prefs.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            startActivity(new Intent(getBaseContext(), MainPreference.class));
            getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
            return true;
        }
    });

    MenuItem share = menu.add("Share");
    share.setIcon(R.drawable.share);
    share.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            getActivity().startActivity(createShareIntent());
            return true;
        }
    });

    MenuItem more = menu.add("More By Developer");
    more.setIcon(R.drawable.morebydev);
    more.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            getActivity().startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://play.google.com/store/apps/developer?id=sachin+shinde")));
            return true;
        }
    });

    MenuItem about = menu.add("About");
    about.setIcon(R.drawable.about);
    about.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            startActivity(new Intent(getBaseContext(), AboutClass.class));
            getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
            return true;
        }
    });

    fakeOverflow.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            return getActivity().onOptionsItemSelected(item);
        }
    });

    menuButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fakeOverflow.show();
        }
    });
}

From source file:com.forrestguice.suntimeswidget.SuntimesActivity.java

/**
 * update actionbar items; shouldn't be called until after the menu is inflated.
 *//*  w  ww.  j  a v a  2s  . co  m*/
private void updateActionBar(Context context) {
    if (actionBarMenu != null) {
        MenuItem refreshItem = actionBarMenu.findItem(R.id.action_location_refresh);
        if (refreshItem != null) {
            WidgetSettings.LocationMode mode = WidgetSettings.loadLocationModePref(context, 0);
            if (mode != WidgetSettings.LocationMode.CURRENT_LOCATION) {
                refreshItem.setVisible(false);

            } else {
                refreshItem.setIcon((getFixHelper.isLocationEnabled() ? GetFixUI.ICON_GPS_FOUND
                        : GetFixUI.ICON_GPS_DISABLED));
                refreshItem.setVisible(true);
            }
        }
    }
}

From source file:com.coincide.alphafitness.ui.Fragment_Main.java

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_split_count:
        Dialog_Split.getDialog(getActivity(), total_start + Math.max(todayOffset + since_boot, 0)).show();
        return true;
    case R.id.action_pause:
        SensorManager sm = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
        Drawable d;//from w  w w .  j  a v  a 2 s.c  o  m
        if (getActivity().getSharedPreferences("pedometer", Context.MODE_PRIVATE).contains("pauseCount")) { // currently paused -> now resumed
            sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),
                    SensorManager.SENSOR_DELAY_UI, 0);
            item.setTitle(R.string.pause);
            d = getResources().getDrawable(R.drawable.ic_pause);
        } else {
            sm.unregisterListener(this);
            item.setTitle(R.string.resume);
            d = getResources().getDrawable(R.drawable.ic_resume);
        }
        d.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
        item.setIcon(d);
        getActivity().startService(new Intent(getActivity(), SensorListener.class).putExtra("action",
                SensorListener.ACTION_PAUSE));
        return true;
    default:
        return ((Activity_Main) getActivity()).optionsItemSelected(item);
    }
}

From source file:org.openintents.notepad.NoteEditor.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    // Build the menus that are shown when editing.

    menu.add(5, MENU_SEARCH, 0, R.string.menu_search).setShortcut('3', 'f')
            .setIcon(android.R.drawable.ic_menu_search);

    // if (!mOriginalContent.equals(mText.getText().toString())) {

    menu.add(0, MENU_REVERT, 0, R.string.menu_revert).setShortcut('0', 'r')
            .setIcon(android.R.drawable.ic_menu_revert);

    menu.add(1, MENU_ENCRYPT, 0, R.string.menu_encrypt).setShortcut('1', 'e')
            .setIcon(android.R.drawable.ic_lock_lock); // TODO:
    // better/*from w ww  .j  a  va2 s  .com*/
    // icon

    menu.add(1, MENU_UNENCRYPT, 0, R.string.menu_undo_encryption).setShortcut('1', 'e')
            .setIcon(android.R.drawable.ic_lock_lock); // TODO:
    // better
    // icon

    MenuItem item = menu.add(1, MENU_DELETE, 0, R.string.menu_delete);
    item.setIcon(android.R.drawable.ic_menu_delete);

    menu.add(2, MENU_IMPORT, 0, R.string.menu_import).setShortcut('1', 'i')
            .setIcon(android.R.drawable.ic_menu_add);

    menu.add(2, MENU_SAVE, 0, R.string.menu_save).setShortcut('2', 's')
            .setIcon(android.R.drawable.ic_menu_save);

    menu.add(2, MENU_SAVE_AS, 0, R.string.menu_save_as).setShortcut('3', 'w')
            .setIcon(android.R.drawable.ic_menu_save);

    menu.add(3, MENU_THEME, 0, R.string.menu_theme).setIcon(android.R.drawable.ic_menu_manage).setShortcut('4',
            't');

    menu.add(3, MENU_SETTINGS, 0, R.string.settings).setIcon(android.R.drawable.ic_menu_preferences)
            .setShortcut('9', 'p');

    item = menu.add(4, MENU_SEND, 0, R.string.menu_share);
    item.setIcon(android.R.drawable.ic_menu_share);
    if (mActionBarAvailable) {
        WrapActionBar.showIfRoom(item);
    }

    menu.add(5, MENU_WORD_COUNT, 0, R.string.menu_word_count);

    /*
       * if (mState == STATE_EDIT) {
     *
     * menu.add(0, REVERT_ID, 0, R.string.menu_revert) .setShortcut('0',
     * 'r') .setIcon(android.R.drawable.ic_menu_revert);
     *
     * if (!mNoteOnly) { menu.add(1, DELETE_ID, 0, R.string.menu_delete)
     * .setShortcut('1', 'd') .setIcon(android.R.drawable.ic_menu_delete); }
     *
     * // Build the menus that are shown when inserting. } else {
     * menu.add(1, DISCARD_ID, 0, R.string.menu_discard) .setShortcut('0',
     * 'd') .setIcon(android.R.drawable.ic_menu_delete); }
     */

    // If we are working on a full note, then append to the
    // menu items for any other activities that can do stuff with it
    // as well. This does a query on the system for any activities that
    // implement the ALTERNATIVE_ACTION for our data, adding a menu item
    // for each one that is found.
    if (!mNoteOnly) {
        // We use mUri instead of getIntent().getData() in the
        // following line, because mUri may have changed when inserting
        // a new note.
        Intent intent = new Intent(null, mUri);
        intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
        // menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
        // new ComponentName(this, NoteEditor.class), null, intent, 0,
        // null);

        // Workaround to add icons:
        MenuIntentOptionsWithIcons menu2 = new MenuIntentOptionsWithIcons(this, menu);
        menu2.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, new ComponentName(this, NoteEditor.class), null,
                intent, 0, null);

        // Add menu items for category CATEGORY_TEXT_SELECTION_ALTERNATIVE
        intent = new Intent(); // Don't pass data for this intent
        intent.addCategory(NotepadIntents.CATEGORY_TEXT_SELECTION_ALTERNATIVE);
        intent.setType("text/plain");
        // Workaround to add icons:
        menu2.addIntentOptions(GROUP_ID_TEXT_SELECTION_ALTERNATIVE, 0, 0,
                new ComponentName(this, NoteEditor.class), null, intent, 0, null);

    }

    return true;
}

From source file:org.uguess.android.sysinfo.ApplicationManager.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuItem mi = menu.add(Menu.NONE, MI_DELETE, Menu.NONE, R.string.uninstall);
    mi.setIcon(android.R.drawable.ic_menu_delete);
    Util.setShowAsAction(mi, MenuItem.SHOW_AS_ACTION_IF_ROOM);

    mi = menu.add(Menu.NONE, MI_SHARE, Menu.NONE, R.string.share);
    mi.setIcon(android.R.drawable.ic_menu_share);
    Util.setShowAsAction(mi, MenuItem.SHOW_AS_ACTION_IF_ROOM);

    if (Util.getSettingsIntent(getActivity().getPackageManager(), "com.android.settings.UsageStats") != null) //$NON-NLS-1$
    {//from   ww  w  .  j  ava 2s .co  m
        mi = menu.add(Menu.NONE, MI_USAGE_STATS, Menu.NONE, R.string.usage_stats);
        mi.setIcon(android.R.drawable.ic_menu_recent_history);
        Util.setShowAsAction(mi, MenuItem.SHOW_AS_ACTION_NEVER);
    }

    mi = menu.add(Menu.NONE, MI_REVERT, Menu.NONE, R.string.restore);
    mi.setIcon(android.R.drawable.ic_menu_revert);
    Util.setShowAsAction(mi, MenuItem.SHOW_AS_ACTION_ALWAYS);

    mi = menu.add(Menu.NONE, MI_PREFERENCE, Menu.NONE, R.string.preference);
    mi.setIcon(android.R.drawable.ic_menu_preferences);
    Util.setShowAsAction(mi, MenuItem.SHOW_AS_ACTION_NEVER);
}

From source file:group.pals.android.lib.ui.filechooser.FragmentFiles.java

@Override
public void onPrepareOptionsMenu(Menu menu) {
    if (BuildConfig.DEBUG)
        Log.d(CLASSNAME, "onPrepareOptionsMenu()");

    /*//from  w  ww . java  2  s.  c o m
     * Some bugs? This method seems to be called even after `onDestroy()`...
     */
    if (getActivity() == null)
        return;

    /*
     * Sorting.
     */

    final boolean sortAscending = DisplayPrefs.isSortAscending(getActivity());
    MenuItem miSort = menu.findItem(R.id.afc_menuitem_sort);

    switch (DisplayPrefs.getSortType(getActivity())) {
    case BaseFile.SORT_BY_NAME:
        miSort.setIcon(Ui.resolveAttribute(getActivity(),
                sortAscending ? R.attr.afc_ic_menu_sort_by_name_asc : R.attr.afc_ic_menu_sort_by_name_desc));
        break;
    case BaseFile.SORT_BY_SIZE:
        miSort.setIcon(Ui.resolveAttribute(getActivity(),
                sortAscending ? R.attr.afc_ic_menu_sort_by_size_asc : R.attr.afc_ic_menu_sort_by_size_desc));
        break;
    case BaseFile.SORT_BY_MODIFICATION_TIME:
        miSort.setIcon(Ui.resolveAttribute(getActivity(),
                sortAscending ? R.attr.afc_ic_menu_sort_by_date_asc : R.attr.afc_ic_menu_sort_by_date_desc));
        break;
    }

    /*
     * View type.
     */

    MenuItem menuItem = menu.findItem(R.id.afc_menuitem_switch_viewmode);
    switch (DisplayPrefs.getViewType(getActivity())) {
    case GRID:
        menuItem.setIcon(Ui.resolveAttribute(getActivity(), R.attr.afc_ic_menu_listview));
        menuItem.setTitle(R.string.afc_cmd_list_view);
        break;
    case LIST:
        menuItem.setIcon(Ui.resolveAttribute(getActivity(), R.attr.afc_ic_menu_gridview));
        menuItem.setTitle(R.string.afc_cmd_grid_view);
        break;
    }

    /*
     * New folder.
     */

    menu.findItem(R.id.afc_menuitem_new_folder).setEnabled(!mLoading);
}