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.alfresco.mobile.android.application.fragments.user.UserProfileFragment.java

private void getMenu(Menu menu, int mode) {
    MenuItem mi = null;
    switch (mode) {
    case MODE_CALL:
        if (person.getTelephoneNumber() != null && !person.getTelephoneNumber().isEmpty()) {
            mi = menu.add(Menu.NONE, R.id.menu_user_phone, Menu.FIRST + 6, person.getTelephoneNumber());
            mi.setIcon(R.drawable.ic_call);
            mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }//from w  w w. j ava  2  s .  c o m

        if (person.getMobileNumber() != null && !person.getMobileNumber().isEmpty()) {
            mi = menu.add(Menu.NONE, R.id.menu_user_mobile, Menu.FIRST + 7, person.getMobileNumber());
            mi.setIcon(R.drawable.ic_call);
            mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }

        if (person.getCompany() != null && person.getCompany().getTelephoneNumber() != null
                && !person.getCompany().getTelephoneNumber().isEmpty()) {
            mi = menu.add(Menu.NONE, R.id.menu_user_company_phone, Menu.FIRST + 5,
                    person.getCompany().getTelephoneNumber());
            mi.setIcon(R.drawable.ic_call);
            mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }
        break;
    case MODE_SKYPE:
        mi = menu.add(Menu.NONE, R.id.menu_user_chat, Menu.FIRST, R.string.start_chat);
        mi.setIcon(R.drawable.ic_im);
        mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        mi = menu.add(Menu.NONE, R.id.menu_user_call, Menu.FIRST + 1, R.string.start_call);
        mi.setIcon(R.drawable.ic_call);
        mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        mi = menu.add(Menu.NONE, R.id.menu_user_videocall, Menu.FIRST + 2, R.string.start_video_call);
        mi.setIcon(R.drawable.ic_videos);
        mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        break;
    case MODE_EMAIL:
        if (person.getEmail() != null && !person.getEmail().isEmpty()) {
            mi = menu.add(Menu.NONE, R.id.menu_user_email, Menu.FIRST + 3, person.getEmail());
            mi.setIcon(R.drawable.ic_send_mail);
            mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }

        if (person.getCompany() != null && person.getCompany().getEmail() != null
                && !person.getCompany().getEmail().isEmpty()) {
            mi = menu.add(Menu.NONE, R.id.menu_user_company_email, Menu.FIRST + 4,
                    person.getCompany().getEmail());
            mi.setIcon(R.drawable.ic_send_mail);
            mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }
        break;

    default:
        break;
    }

}

From source file:com.awadev.itslearningautologin.MainActivity.java

/**
 * Action bar buttons/*from  ww w  .j a v  a  2  s  .  c om*/
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.
    // ActionBarDrawerToggle will take care of this.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    // Handle action buttons
    switch (item.getItemId()) {
    case R.id.action_toggle_menu:
        mWebView.loadUrl("javascript:$(\"#ctl00_BottomNavigationBar_ArrowButton\").click();");

        mToggleMenu = !mToggleMenu;
        if (mToggleMenu)
            item.setIcon(R.drawable.ic_action_collapse);
        else
            item.setIcon(R.drawable.ic_action_expand);
        return true;

    case R.id.action_toggle_javascript:
        mWebView.getSettings().setJavaScriptEnabled(!mWebView.getSettings().getJavaScriptEnabled());
        mWebView.reload();
        Toast.makeText(this,
                "Javascript: " + (mWebView.getSettings().getJavaScriptEnabled() ? getString(R.string.on)
                        : getString(R.string.off)),
                Toast.LENGTH_SHORT).show();
        return true;

    case R.id.action_refresh:
        mWebView.reload();
        Toast.makeText(this, getString(R.string.toast_refresh), Toast.LENGTH_SHORT).show();
        return true;

    //            case R.id.action_profile_dropdown:
    //                mWebView.loadUrl("javascript:$(\"#personal-menu-dd\").toggle();");
    //                return true;
    //
    //            case R.id.action_mail_dropdown:
    //                mWebView.loadUrl("javascript:$(\"#ctl00_PersonalResponsiveMenu_InboxLink\").click();");
    //                return true;
    //
    //            case R.id.action_notification_dropdown:
    //                mWebView.loadUrl("javascript:$(\"#ctl00_PersonalResponsiveMenu_NotificationLink\").click();");
    //                return true;

    case R.id.action_settings:
        Toast.makeText(this, getResources().getText(R.string.no_settings), Toast.LENGTH_SHORT).show();
        return true;

    case R.id.action_logout:
        SharedPreferences sharedPref = getSharedPreferences(LoginActivity.PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.remove(getString(R.string.password_key));
        editor.commit();

        WebComponent.mCookieStore.clear();

        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
        return true;

    case R.id.action_licenses:
        LicensedProject showLicense = new LicensedProject("showlicense", null,
                "https://github.com/behumble/showlicense", License.APACHE2);
        LicensedProject asyncHttp = new LicensedProject("Android Asynchronous Http Client", null,
                "http://loopj.com/android-async-http/", License.APACHE2);
        LicensedProject jSoup = new LicensedProject("jsoup", null, "http://jsoup.org/", License.MIT);

        LicensedProject[] projList = new LicensedProject[] { showLicense, asyncHttp, jSoup };
        ShowLicense.createDialog(this, getString(R.string.title_licenses), projList).show();
        return true;

    default:
        Log.i("AWA", "MenuItem clicked: " + item.getItemId());
        return super.onOptionsItemSelected(item);
    }
}

From source file:gov.cdc.epiinfo.RecordList.java

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

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView = new SearchView(this);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override//  w ww  .  j ava 2s . com
        public boolean onQueryTextSubmit(String searchTerm) {

            try {
                mDbHelper.fetchTopOne();

                String query = BuildQuery(searchTerm);

                String fieldName1;
                String fieldName2;
                String fieldName3;
                String[] from = new String[1];
                int[] to = new int[1];

                if (formMetadata.DataFields.size() > 2) {

                    fieldName1 = formMetadata.DataFields.get(0).getName();
                    fieldName2 = formMetadata.DataFields.get(1).getName();
                    fieldName3 = formMetadata.DataFields.get(2).getName();
                    mNotesCursor = mDbHelper.fetchWhere(fieldName1, fieldName2, fieldName3, query);
                    from = new String[] { "_id", "columnName1", fieldName1, "columnName2", fieldName2,
                            "columnName3", fieldName3, "_syncStatus" };
                    to = new int[] { R.id.text1, R.id.header2, R.id.text2, R.id.header3, R.id.text3,
                            R.id.header4, R.id.text4, R.id.hiddenText };
                } else if (formMetadata.DataFields.size() == 2) {
                    fieldName1 = formMetadata.DataFields.get(0).getName();
                    fieldName2 = formMetadata.DataFields.get(1).getName();
                    mNotesCursor = mDbHelper.fetchWhere(fieldName1, fieldName2, query);
                    from = new String[] { "_id", "columnName1", fieldName1, "columnName2", fieldName2,
                            "_syncStatus" };
                    to = new int[] { R.id.text1, R.id.header2, R.id.text2, R.id.header3, R.id.text3,
                            R.id.hiddenText };
                } else if (formMetadata.DataFields.size() == 1) {
                    fieldName1 = formMetadata.DataFields.get(0).getName();
                    mNotesCursor = mDbHelper.fetchWhere(fieldName1, query);
                    from = new String[] { "_id", "columnName1", fieldName1, "_syncStatus" };
                    to = new int[] { R.id.text1, R.id.header2, R.id.text2, R.id.hiddenText };
                }

                startManagingCursor(mNotesCursor);
                CustomListAdapter notes = new CustomListAdapter(self, R.layout.line_list_row, mNotesCursor,
                        from, to);
                lineListFragment.setListAdapter(notes);

            } catch (Exception ex) {
                fillData();
            }

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            if (newText.equals("")) {
                fillData();
            }

            return false;
        }
    });

    mnuSearch = menu.add(0, SEARCH_ID, 0, R.string.menu_search);
    mnuSearch.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    mnuSearch.setActionView(searchView);
    mnuSearch.setIcon(gov.cdc.epiinfo.R.drawable.action_search);

    MenuItem mnuQR = menu.add(0, QR_ID, 1, R.string.menu_barcode);
    mnuQR.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    mnuQR.setIcon(gov.cdc.epiinfo.R.drawable.qrcode_scan);

    MenuItem mnuCloud = menu.add(0, CLOUD_ID, 2, R.string.menu_cloud_sync);
    mnuCloud.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    MenuItem mnuSync = menu.add(0, SYNC_ID, 3, R.string.menu_sync_file);
    mnuSync.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    MenuItem mnuDeleteAll = menu.add(0, DELETE_ALL_ID, 4, R.string.menu_delete_all);
    mnuDeleteAll.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    if (fkeyGuid == null || fkeyGuid.length() == 0) {
        mnuSetDefault = menu.add(0, SET_DEFAULT_ID, 5, R.string.set_default_form);
        mnuSetDefault.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        mnuExitDefault = menu.add(0, EXIT_DEFAULT_MODE_ID, 5, R.string.exit_default_form);
        mnuExitDefault.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        if (AppManager.getDefaultForm().equals("")) {
            mnuSetDefault.setVisible(true);
            mnuExitDefault.setVisible(false);
        } else {
            mnuSetDefault.setVisible(false);
            mnuExitDefault.setVisible(true);
        }
    }

    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    if (sharedPref.getString("cloud_service", "").equals("Box")) {
        mnuBoxSignin = menu.add(0, BOX_SIGNIN_ID, 6, R.string.box_signin);
        mnuBoxSignin.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        mnuBoxSignout = menu.add(0, BOX_SIGNOUT_ID, 6, R.string.box_signout);
        mnuBoxSignout.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        if (BoxClient.isAuthenticated(this)) {
            mnuBoxSignout.setVisible(true);
            mnuBoxSignin.setVisible(false);
        } else {
            mnuBoxSignout.setVisible(false);
            mnuBoxSignin.setVisible(true);
        }
    }

    MenuItem mnuHelp = menu.add(1, HELP_ID, 7, R.string.menu_help);
    mnuHelp.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return true;
}

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

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuItem 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:info.tellmetime.TellmetimeActivity.java

public void showPopupMenu(View view) {
    PopupMenu popup = new PopupMenu(this, view);

    popup.setOnMenuItemClickListener(this);
    popup.setOnDismissListener(new PopupMenu.OnDismissListener() {
        @Override/*w w  w .  ja v  a  2  s .  c  o  m*/
        public void onDismiss(PopupMenu popupMenu) {
            mHider.delayedHide(4000);
        }
    });

    popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu());

    MenuItem toggle = popup.getMenu().findItem(R.id.action_mode);
    toggle.setTitle(isNightMode ? R.string.action_mode_day : R.string.action_mode_night);
    toggle.setIcon(isNightMode ? R.drawable.ic_action_brightness_high : R.drawable.ic_action_brightness_low);

    popup.show();

    mHider.showNoAutoHide();
}

From source file:th.in.ffc.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.layout.whatnew_dialog:
        String tag = "whatnew";
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment prev = fm.findFragmentByTag(tag);
        WhatnewDialogFragment f;//  w w  w.  j  a  va  2  s .c  o  m
        if (prev != null) {
            f = (WhatnewDialogFragment) prev;
            ft.remove(f);
        } else {
            f = (WhatnewDialogFragment) Fragment.instantiate(this, WhatnewDialogFragment.class.getName(), null);
        }
        ft.addToBackStack(null);
        f.show(fm, tag);

        SharedPreferences.Editor se = getSharedPreferences(FamilyFolderCollector.TAG, MODE_PRIVATE).edit();
        se.putInt(FamilyFolderCollector.PREF_VERSION, getResources().getInteger(R.integer.version_code));
        se.commit();
        item.setIcon(R.drawable.ic_action_start_dark);

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

From source file:org.alfresco.mobile.android.application.fragments.browser.ChildrenBrowserFragment.java

public void getMenu(Menu menu) {
    if (parentFolder == null) {
        return;//from   w  w w .j a v a2 s. c  o  m
    }

    if (getActivity() instanceof MainActivity) {
        getMenu(alfSession, menu, parentFolder);

        if (hasDocument()) {
            displayMenuItem = menu.add(Menu.NONE, MenuActionItem.MENU_DISPLAY_GALLERY,
                    Menu.FIRST + MenuActionItem.MENU_DISPLAY_GALLERY, R.string.display_gallery);
            displayMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        }
    } else if (getActivity() instanceof PublicDispatcherActivity) {
        Permissions permission = alfSession.getServiceRegistry().getDocumentFolderService()
                .getPermissions(parentFolder);

        if (permission.canAddChildren()) {
            MenuItem mi = menu.add(Menu.NONE, MenuActionItem.MENU_CREATE_FOLDER,
                    Menu.FIRST + MenuActionItem.MENU_CREATE_FOLDER, R.string.folder_create);
            mi.setIcon(R.drawable.ic_add_folder);
            mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }
    }

    // Uncomment for enabling view switcher
    /*
     * displayMenuItem = menu.add(Menu.NONE,
     * MenuActionItem.MENU_DISPLAY_ITEMS, Menu.FIRST +
     * MenuActionItem.MENU_DISPLAY_ITEMS, R.string.display_views); switch
     * (displayMode) { case DISPLAY_LIST:
     * displayMenuItem.setIcon(R.drawable.ic_action_list); break; case
     * DISPLAY_LIST_LARGE:
     * displayMenuItem.setIcon(R.drawable.ic_action_tiles_small); break;
     * case DISPLAY_GRID:
     * displayMenuItem.setIcon(R.drawable.ic_action_list_2); break; default:
     * break; }
     * displayMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
     */
}

From source file:com.bt.download.android.gui.activities.AudioPlayerActivity.java

private void setFavoriteIcon(final Menu favorite) {
    final MenuItem favoriteAction = favorite.findItem(R.id.menu_favorite);
    if (favoriteAction != null) {
        if (MusicUtils.isFavorite()) {
            favoriteAction.setIcon(R.drawable.ic_action_favorite_selected);
        } else {/*from  w  ww. ja v a  2  s  .  c o m*/
            favoriteAction.setIcon(R.drawable.ic_action_favorite_normal);
        }
    }
}

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;
        }/*from www.  ja va  2s  .c  o m*/

        @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;
}

From source file:org.spinsuite.view.V_Process.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (isDrawerLoaded) {
        if (m_DToggle.onOptionsItemSelected(item)) {
            return true;
        }/*  w  w  w.  j av  a2 s  . c o  m*/
    }
    //   
    switch (item.getItemId()) {
    case R.id.action_process:
        if (isLoaded) {
            ll_ProcessPara.setVisibility(ScrollView.VISIBLE);
            item.setIcon(R.drawable.process_m);
            if (m_pInfo.isReport()) {
                iSearch.setVisible(false);
                iPrintFormat.setVisible(false);
            }
            //
            isLoaded = false;
            return true;
        }
        //   Run Process
        if (!getValuesFromParameters())
            return false;
        m_pControl.runProcess();
        tv_Summary.setTextAppearance(this, R.style.TextStandard);
        tv_Summary.setVisibility(TextView.VISIBLE);
        //   If Error
        if (m_pInfo.isError())
            tv_Summary.setTextColor(Color.RED);
        else
            tv_Summary.setTextColor(Color.BLUE);
        //   Set Summary
        tv_Summary.setText(m_pInfo.getSummary());
        //   Show all logs
        if (!m_pInfo.isReport() || m_pInfo.isError())
            showLog();
        //   Show report
        else if (m_pInfo.isReport()) {
            iSearch.setVisible(true);
            iPrintFormat.setVisible(true);
            ll_HeaderReport.setVisibility(LinearLayout.VISIBLE);
            showReport(0);
        }
        //   Hide Parameter
        if (!m_pInfo.isError()) {
            ll_ProcessPara.setVisibility(ScrollView.GONE);
            item.setIcon(R.drawable.config_m);
            //   Set Is Loaded
            isLoaded = true;
        }
        return true;
    case R.id.action_print_format:
        showPrintFormat();
        return true;
    }
    return super.onOptionsItemSelected(item);
}