Example usage for android.view Menu findItem

List of usage examples for android.view Menu findItem

Introduction

In this page you can find the example usage for android.view Menu findItem.

Prototype

public MenuItem findItem(int id);

Source Link

Document

Return the menu item with a particular identifier.

Usage

From source file:at.bitfire.davdroid.syncadapter.LoginURLFragment.java

@Override
public void onPrepareOptionsMenu(Menu menu) {
    boolean usernameOk = editUserName.getText().length() > 0, passwordOk = editPassword.getText().length() > 0,
            urlOk = false;/* w ww  .jav a 2 s . co  m*/

    // check host name
    try {
        if (!StringUtils.isBlank(getBaseURI().getHost()))
            urlOk = true;
    } catch (Exception e) {
    }

    MenuItem item = menu.findItem(R.id.next);
    item.setEnabled(usernameOk && passwordOk && urlOk);
}

From source file:com.actionbarsherlock.sample.styled.MainActivityICS.java

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

    // set up a listener for the refresh item
    final MenuItem refresh = (MenuItem) menu.findItem(R.id.menu_refresh);
    refresh.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        // on selecting show progress spinner for 1s
        public boolean onMenuItemClick(MenuItem item) {
            // item.setActionView(R.layout.progress_action);
            handler.postDelayed(new Runnable() {
                public void run() {
                    refresh.setActionView(null);
                }//  w w w . j  a  v  a2  s.com
            }, 1000);
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

From source file:it.scoppelletti.mobilepower.app.CommonMenuFragment.java

/**
 * Creazione del menù.//from w  w w  .  j  a  v  a2  s  . co m
 * 
 * @param menu     Menù
 * @param inflater Parser del menù
 */
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    String pkgName;
    MenuItem menuItem;

    pkgName = AppUtils.getFullPackageName(getActivity(), true);

    if (StringUtils.isBlank(pkgName)) {
        inflater.inflate(R.menu.common, menu);
    } else {
        inflater.inflate(R.menu.demo, menu);

        menuItem = menu.findItem(R.id.cmd_buy);
        MenuItemCompat.setShowAsAction(menuItem, MenuItemCompat.SHOW_AS_ACTION_NEVER);
    }

    menuItem = menu.findItem(R.id.cmd_settings);
    MenuItemCompat.setShowAsAction(menuItem, MenuItemCompat.SHOW_AS_ACTION_NEVER);

    menuItem = menu.findItem(R.id.cmd_help);
    MenuItemCompat.setShowAsAction(menuItem, MenuItemCompat.SHOW_AS_ACTION_NEVER);

    menuItem = menu.findItem(R.id.cmd_about);
    MenuItemCompat.setShowAsAction(menuItem, MenuItemCompat.SHOW_AS_ACTION_NEVER);
}

From source file:app.socialgps.ui.MainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // If the nav drawer is open, hide action items related to the content
    // view/*from  w ww.j ava2 s. c om*/
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    menu.findItem(R.id.action_notifications).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

From source file:ca.zadrox.dota2esportticker.ui.TeamSearchActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.team_search, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    Toolbar.LayoutParams params = new Toolbar.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT,
            Toolbar.LayoutParams.MATCH_PARENT);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    mSearchView = (SearchView) searchItem.getActionView();
    mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    mSearchView.setLayoutParams(params);
    mSearchView.setMaxWidth(getResources().getDisplayMetrics().widthPixels);

    mSearchView.setIconifiedByDefault(false);
    mSearchView.setQueryHint("Search");

    ImageView magImage = (ImageView) mSearchView.findViewById(R.id.search_mag_icon);
    magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0));

    final EditText editText = (AutoCompleteTextView) mSearchView.findViewById(R.id.search_src_text);
    editText.setHintTextColor(getResources().getColor(R.color.theme_primary_light));

    if (mQuery != null) {
        mSearchView.setQuery(mQuery, false);
    } else {//from w  w  w . j a v  a 2 s  . co m
        mSearchView.requestFocus();
    }

    mSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
        @Override
        public boolean onSuggestionSelect(int i) {
            return onSuggestionClick(i);
        }

        @Override
        public boolean onSuggestionClick(int i) {
            Cursor c = (Cursor) mSearchView.getSuggestionsAdapter().getItem(i);
            mSearchView.setQuery(c.getString(1), true);
            mSearchView.clearFocus();

            return true;
        }
    });

    return true;
}

From source file:com.android.deskclock.DeskClock.java

private void showMenu(View v) {
    PopupMenu popupMenu = new PopupMenu(this, v);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override/*from   w ww  .  j  a  va 2s.  c o m*/
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.menu_item_settings:
                startActivity(new Intent(DeskClock.this, SettingsActivity.class));
                return true;
            case R.id.menu_item_help:
                Intent i = item.getIntent();
                if (i != null) {
                    try {
                        startActivity(i);
                    } catch (ActivityNotFoundException e) {
                        // No activity found to match the intent - ignore
                    }
                }
                return true;
            case R.id.menu_item_night_mode:
                startActivity(new Intent(DeskClock.this, ScreensaverActivity.class));
            default:
                break;
            }
            return true;
        }
    });
    popupMenu.inflate(R.menu.desk_clock_menu);

    Menu menu = popupMenu.getMenu();
    MenuItem help = menu.findItem(R.id.menu_item_help);
    if (help != null) {
        Utils.prepareHelpMenuItem(this, help);
    }
    popupMenu.show();
}

From source file:at.bitfire.davdroid.syncadapter.SelectCollectionsFragment.java

@Override
public void onPrepareOptionsMenu(Menu menu) {
    boolean ok = false;
    try {/*  w  w  w .j  av  a 2 s . co m*/
        ListView listView = getListView();
        for (int i = listView.getCount() - 1; i >= 0; i--) {
            if (listView.isItemChecked(i)) {
                ok = true;
                break;
            }
        }
    } catch (IllegalStateException e) {
    }
    MenuItem item = menu.findItem(R.id.next);
    item.setEnabled(ok);
}

From source file:ch.ethz.inf.vs.a1.fabischn.ble.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Log.v("Menu Created:", "onCreateOptionsMenu");
    getMenuInflater().inflate(R.menu.main, menu);
    if (!mScanning) {
        menu.findItem(R.id.menu_stop).setVisible(false);
        menu.findItem(R.id.menu_scan).setVisible(true);
        menu.findItem(R.id.menu_refresh).setActionView(null);
    } else {/*from   w w  w  . j a va  2s  . c  o  m*/
        menu.findItem(R.id.menu_stop).setVisible(true);
        menu.findItem(R.id.menu_scan).setVisible(false);
        menu.findItem(R.id.menu_refresh).setActionView(R.layout.actionbar_indeterminate_progress);
    }
    return true;
}

From source file:edu.nd.darts.cimon.NDroidAdmin.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    try {/*  w w  w .  j  av a 2  s. com*/
        inflater.inflate(R.menu.options_menu, menu);
    } catch (InflateException e) {
        e.printStackTrace();
        return false;
    }
    MenuItem item = menu.findItem(R.id.startup);
    if (item != null) {
        SharedPreferences appPrefs = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
        boolean startup = appPrefs.getBoolean(PREF_STARTUP, true);
        item.setChecked(startup);
    }
    return true;
}

From source file:com.adrguides.ReadGuideFragment.java

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    menu.findItem(R.id.action_viewguide)
            .setEnabled(ttsfragment.getGuide() != null && ttsfragment.getGuide().getAddress() != null);

    if (ttsfragment.isTTSReady() && ttsfragment.isGuideAvailable() && ttsfragment.isGuideLanguageAvailable()) {

        boolean playing = ttsfragment.isPlaying();

        menu.findItem(R.id.action_playpause).setEnabled(true)
                .setTitle(playing ? R.string.action_stop : R.string.action_play)
                .setIcon(playing ? R.drawable.ic_media_stop : R.drawable.ic_media_play);
        menu.findItem(R.id.menu_search).setEnabled(true).setIcon(R.drawable.ic_menu_search);
        if (ttsfragment.isEnabledNext()) {
            menu.findItem(R.id.action_next).setEnabled(true).setIcon(R.drawable.ic_media_next);
        } else {/*www .  j  a v  a  2  s.  com*/
            menu.findItem(R.id.action_next).setEnabled(false)
                    .setIcon(getDrawableDisabled(R.drawable.ic_media_next));
        }
        if (ttsfragment.isEnabledPrevious()) {
            menu.findItem(R.id.action_previous).setEnabled(true).setIcon(R.drawable.ic_media_previous);
        } else {
            menu.findItem(R.id.action_previous).setEnabled(false)
                    .setIcon(getDrawableDisabled(R.drawable.ic_media_previous));
        }
        menu.findItem(R.id.action_bookmark).setEnabled(!ttsfragment.getGuide().isStored());
        menu.findItem(R.id.action_list).setEnabled(true);
    } else {
        menu.findItem(R.id.action_playpause).setEnabled(false)
                .setIcon(getDrawableDisabled(R.drawable.ic_media_play));
        menu.findItem(R.id.menu_search).setEnabled(false)
                .setIcon(getDrawableDisabled(R.drawable.ic_menu_search));
        menu.findItem(R.id.action_next).setEnabled(false)
                .setIcon(getDrawableDisabled(R.drawable.ic_media_next));
        menu.findItem(R.id.action_previous).setEnabled(false)
                .setIcon(getDrawableDisabled(R.drawable.ic_media_previous));
        menu.findItem(R.id.action_bookmark).setEnabled(false);
        menu.findItem(R.id.action_list).setEnabled(false);
    }
}