Example usage for android.view MenuItem setChecked

List of usage examples for android.view MenuItem setChecked

Introduction

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

Prototype

public MenuItem setChecked(boolean checked);

Source Link

Document

Control whether this item is shown with a check mark.

Usage

From source file:github.daneren2005.dsub.activity.SubsonicActivity.java

@Override
public void setContentView(int viewId) {
    if (isTv()) {
        super.setContentView(R.layout.static_drawer_activity);
    } else {// ww  w  .  ja  v  a  2s .c  o m
        super.setContentView(R.layout.abstract_activity);
    }
    rootView = (ViewGroup) findViewById(R.id.content_frame);

    if (viewId != 0) {
        LayoutInflater layoutInflater = getLayoutInflater();
        layoutInflater.inflate(viewId, rootView);
    }

    drawerList = (NavigationView) findViewById(R.id.left_drawer);
    drawerList.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(final MenuItem menuItem) {
            if (showingTabs) {
                // Settings are on a different selectable track
                if (menuItem.getItemId() != R.id.drawer_settings && menuItem.getItemId() != R.id.drawer_admin
                        && menuItem.getItemId() != R.id.drawer_offline) {
                    menuItem.setChecked(true);
                    lastSelectedPosition = menuItem.getItemId();
                }

                switch (menuItem.getItemId()) {
                case R.id.drawer_home:
                    drawerItemSelected("Home");
                    return true;
                case R.id.drawer_library:
                    drawerItemSelected("Artist");
                    return true;
                case R.id.drawer_playlists:
                    drawerItemSelected("Playlist");
                    return true;
                case R.id.drawer_podcasts:
                    drawerItemSelected("Podcast");
                    return true;
                case R.id.drawer_bookmarks:
                    drawerItemSelected("Bookmark");
                    return true;
                case R.id.drawer_shares:
                    drawerItemSelected("Share");
                    return true;
                case R.id.drawer_chat:
                    drawerItemSelected("Chat");
                    return true;
                case R.id.drawer_admin:
                    if (UserUtil.isCurrentAdmin()) {
                        UserUtil.confirmCredentials(SubsonicActivity.this, new Runnable() {
                            @Override
                            public void run() {
                                drawerItemSelected("Admin");
                                menuItem.setChecked(true);
                                lastSelectedPosition = menuItem.getItemId();
                            }
                        });
                    } else {
                        drawerItemSelected("Admin");
                        menuItem.setChecked(true);
                        lastSelectedPosition = menuItem.getItemId();
                    }
                    return true;
                case R.id.drawer_downloading:
                    drawerItemSelected("Download");
                    return true;
                case R.id.drawer_offline:
                    toggleOffline();
                    return true;
                case R.id.drawer_settings:
                    startActivity(new Intent(SubsonicActivity.this, SettingsActivity.class));
                    drawer.closeDrawers();
                    return true;
                }
            } else {
                int activeServer = menuItem.getItemId() - MENU_ITEM_SERVER_BASE;
                SubsonicActivity.this.setActiveServer(activeServer);
                populateTabs();
                return true;
            }

            return false;
        }
    });

    drawerHeader = drawerList.inflateHeaderView(R.layout.drawer_header);
    drawerHeader.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (showingTabs) {
                populateServers();
            } else {
                populateTabs();
            }
        }
    });

    drawerHeaderToggle = (ImageView) drawerHeader.findViewById(R.id.header_select_image);
    drawerServerName = (TextView) drawerHeader.findViewById(R.id.header_server_name);
    drawerUserName = (TextView) drawerHeader.findViewById(R.id.header_user_name);

    drawerUserAvatar = (ImageView) drawerHeader.findViewById(R.id.header_user_avatar);

    updateDrawerHeader();

    if (!isTv()) {
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        // Pass in toolbar if it exists
        Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
        drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.common_appname,
                R.string.common_appname) {
            @Override
            public void onDrawerClosed(View view) {
                setTitle(currentFragment.getTitle());

                drawerIdle = true;
                drawerOpen = false;

                supportInvalidateOptionsMenu();
                if (!showingTabs) {
                    populateTabs();
                }
            }

            @Override
            public void onDrawerOpened(View view) {
                DownloadService downloadService = getDownloadService();
                boolean downloadingVisible = downloadService != null
                        && !downloadService.getBackgroundDownloads().isEmpty();
                if (lastSelectedPosition == R.id.drawer_downloading) {
                    downloadingVisible = true;
                }
                setDrawerItemVisible(R.id.drawer_downloading, downloadingVisible);

                getSupportActionBar().setTitle(R.string.common_appname);
                getSupportActionBar().setDisplayShowCustomEnabled(false);

                drawerIdle = true;
                drawerOpen = true;

                supportInvalidateOptionsMenu();
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
                drawerIdle = false;
            }
        };
        drawer.setDrawerListener(drawerToggle);
        drawerToggle.setDrawerIndicatorEnabled(true);

        drawer.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (drawerIdle && currentFragment != null && currentFragment.getGestureDetector() != null) {
                    return currentFragment.getGestureDetector().onTouchEvent(event);
                } else {
                    return false;
                }
            }
        });
    }

    // Check whether this is a tablet or not
    secondaryContainer = findViewById(R.id.fragment_second_container);
    if (secondaryContainer != null) {
        primaryContainer = findViewById(R.id.fragment_container);
    }
}

From source file:com.techmighty.baseplayer.activities.MainActivity.java

private void updatePosition(final MenuItem menuItem) {
    runnable = null;/*ww w . j  a  v  a 2s .co  m*/

    switch (menuItem.getItemId()) {
    case R.id.nav_library:
        runnable = navigateLibrary;

        break;
    case R.id.nav_playlists:
        runnable = navigatePlaylist;

        break;
    case R.id.nav_nowplaying:
        NavigationUtils.navigateToNowplaying(MainActivity.this, false);
        break;
    case R.id.nav_queue:
        runnable = navigateQueue;

        break;
    case R.id.nav_settings:
        NavigationUtils.navigateToSettings(MainActivity.this);
        break;
    case R.id.nav_help:
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data = Uri.parse("mailto:techmightian@gmail.com");
        intent.setData(data);
        startActivity(intent);
        break;
    case R.id.nav_about:
        mDrawerLayout.closeDrawers();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Helpers.showAbout(MainActivity.this);
            }
        }, 350);

        break;
    }

    if (runnable != null) {
        menuItem.setChecked(true);
        mDrawerLayout.closeDrawers();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                runnable.run();
            }
        }, 350);
    }
}

From source file:com.google.android.apps.forscience.whistlepunk.project.ProjectDetailsFragment.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        Intent upIntent = NavUtils.getParentActivityIntent(getActivity());
        upIntent.putExtra(MainActivity.ARG_SELECTED_NAV_ITEM_ID, R.id.navigation_item_projects);
        upIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(),
                mProjectCover, mProjectId);
        getActivity().startActivity(upIntent, options.toBundle());
        return true;
    } else if (id == R.id.action_edit_project) {
        UpdateProjectActivity.launch(getActivity(), mProjectId, false /* not new */);
        return true;
    } else if (id == R.id.action_archive_project || id == R.id.action_unarchive_project) {
        setProjectArchived(id == R.id.action_archive_project);
        return true;

    } else if (id == R.id.action_delete_project) {
        confirmDelete();//from www . ja v  a2  s  . c  o  m
        return true;
    } else if (id == R.id.action_include_archived) {
        item.setChecked(!item.isChecked());
        mIncludeArchived = item.isChecked();
        loadExperiments();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

From source file:com.murrayc.galaxyzoo.app.QuestionFragment.java

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    // handle item selection
    switch (item.getItemId()) {
    case R.id.option_menu_item_examples:
        doExamples();/*from  w  w  w . j  a  v  a2  s.c o  m*/
        return true;
    case R.id.option_menu_item_favorite:
        final boolean checked = !item.isChecked();

        //Note:
        //"Icon menu" (TODO: What is that?) items don't actually show a checked state,
        //but it seems to work in the menu though not as an action in the action bar.
        //See http://developer.android.com/guide/topics/ui/menus.html#checkable
        item.setChecked(checked);

        //TODO: Use pretty icons instead:
        /*
        //Show an icon to indicate checkedness instead:
        //See http://developer.android.com/guide/topics/ui/menus.html#checkable
        if (checked) {
            item.setIcon(android.R.drawable.ic_menu_save); //A silly example.
        } else {
            item.setIcon(android.R.drawable.ic_menu_add); //A silly example.
        }
        */

        mClassificationInProgress.setFavorite(checked);
        return true;
    case R.id.option_menu_item_restart:
        restartClassification();
        return true;
    case R.id.option_menu_item_discuss:
        UiUtils.openDiscussionPage(getActivity(), getZooniverseId());
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // TODO handle clicking the app icon/logo
        return false;
    case R.id.menu_refresh:
        // switch to a progress animation
        item.setActionView(R.layout.indeterminate_progress_action);
        return true;
    case R.id.menu_both:
        // rotation animation of green fragment
        //            rotateLeftFrag();
        return true;
    case R.id.menu_text:
        // alpha animation of blue fragment
        //            ObjectAnimator alpha = ObjectAnimator.ofFloat(rightFrag.getView(),
        //                    "alpha", 1f, 0f);
        //            alpha.setRepeatMode(ObjectAnimator.REVERSE);
        //            alpha.setRepeatCount(1);
        //            alpha.setDuration(800);
        //            alpha.start();
        return true;
    case R.id.menu_logo:
        useLogo = !useLogo;/* www  . jav  a 2s.  c  o  m*/
        //item.setChecked(useLogo);
        getActionBar().setDisplayUseLogoEnabled(useLogo);
        return true;
    case R.id.menu_up:
        showHomeUp = !showHomeUp;
        //item.setChecked(showHomeUp);
        getActionBar().setDisplayHomeAsUpEnabled(showHomeUp);
        return true;
    case R.id.menu_nav_tabs:
        item.setChecked(true);
        showTabsNav();
        return true;
    //        case R.id.menu_nav_label:
    //            item.setChecked(true);
    //            showStandardNav();
    //            return true;
    case R.id.menu_nav_drop_down:
        item.setChecked(true);
        showDropDownNav();
        return true;
    //        case R.id.menu_bak_none:
    //            item.setChecked(true);
    //            getSupportActionBar().setBackgroundDrawable(null);
    //            return true;
    //        case R.id.menu_bak_gradient:
    //            item.setChecked(true);
    //            getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ad_action_bar_gradient_bak));
    //            return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

From source file:org.feedhenry.welcome.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // -- Toolbar
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);/*w  w w .j a v a2  s.  c  om*/

    // -- Actionbar
    final ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    // -- Drawer & NavigationView
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            Fragment fragment = null;

            switch (menuItem.getItemId()) {
            case R.id.drawer_home:
                fragment = new HomeFragment();
                break;
            case R.id.drawer_cloud:
                fragment = new CloudFragment();
                break;
            case R.id.drawer_push:
                fragment = new PushFragment();
                break;
            case R.id.drawer_location:
                fragment = new LocationFragment();
                break;
            case R.id.drawer_data:
                fragment = new DataBrowerFragment();
                break;
            case R.id.drawer_info:
                fragment = new NativeAppInfoFragment();
                break;
            case R.id.drawer_integration:
                fragment = new IntegrationFragment();
                break;
            case R.id.drawer_statistics:
                fragment = new StatisticsFragment();
                break;
            }

            if (fragment != null) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();

                menuItem.setChecked(true);
            }

            drawerLayout.closeDrawers();
            return true;
        }
    });

}

From source file:com.hangulo.powercontact.MainActivity.java

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) { //Navigation? ?? ?? ? 

    // update the main content by replacing fragments
    // ?  ?? ?   ?? .
    //Intent intent;
    int mSelectedId;
    menuItem.setChecked(true);
    mSelectedId = menuItem.getItemId();/*from  w ww.ja  va 2 s .c  o  m*/
    switch (mSelectedId) {

    case R.id.drawer_menu_header:
        mDrawerLayout.closeDrawer(GravityCompat.START); //Drawer ?
        return true;

    case R.id.main_drawer_item_01: { // ?
        mDrawerLayout.closeDrawer(GravityCompat.START); //Drawer ?
        selectAccount(true); // start sync
        return true;
    }
    case R.id.main_drawer_item_02: // ??
    // Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
    {
        mDrawerLayout.closeDrawer(GravityCompat.START); //Drawer ?

        Intent intentNext = new Intent(this, ErrorContactsListActivity.class);
        startActivity(intentNext);
        return true;
    }

    case R.id.main_drawer_item_04: { //  Settings
        mDrawerLayout.closeDrawer(GravityCompat.START);
        Intent SettingActivity = new Intent(this, SettingsActivity.class);
        startActivityForResult(SettingActivity, Constants.SETTINGS_REQUEST); // when settings changed then apply it immediately
        return true;
    }

    case R.id.main_drawer_item_05: // about
        mDrawerLayout.closeDrawer(GravityCompat.START); //Drawer ?
        OpensourceFragment.displayLicensesFragment(getSupportFragmentManager());
        return true;
    }
    return false;
}

From source file:com.openatk.rockapp.MainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    if (mCurrentState == STATE_DEFAULT) {
        menu.clear();/*from w w  w .j  av a 2  s .c o m*/
        inflater.inflate(R.menu.main, menu);

        MenuItem showHideItem = menu.findItem(R.id.show_hide);
        MenuItem currentShowHideItem;

        switch (mRockState) {
        case STATE_ROCKS_BOTH:
            currentShowHideItem = menu.findItem(R.id.all_rocks);
            break;

        case STATE_ROCKS_NOT_PICKED_UP:
            currentShowHideItem = menu.findItem(R.id.not_picked_rocks);
            break;

        case STATE_ROCKS_PICKED_UP:
            currentShowHideItem = menu.findItem(R.id.picked_rocks);
            break;
        default:
            // We are some how lost, just revert back to showing everything
            Log.e("MainActivity", "Lost Rock Shown State");
            mRockState = STATE_ROCKS_BOTH;
            currentShowHideItem = menu.findItem(R.id.all_rocks);
            break;
        }

        // Copy the current selection to the action bar
        showHideItem.setTitle(currentShowHideItem.getTitle());

        // Mark the current one as checked
        currentShowHideItem.setChecked(true);

        // The location button changes depending the current state
        // of location

        // TODO
        /*
         * MenuItem gps = menu.findItem(R.id.gps); if(map.ha) {
         * if(mRockLocationManager.haveUserLocation()) {
         * gps.setIcon(R.drawable.gps_found);
         * gps.setTitle(R.string.menu_gps); } else {
         * gps.setIcon(R.drawable.gps_searching);
         * gps.setTitle(R.string.menu_gps_searching); } } else {
         * gps.setIcon(R.drawable.gps_off);
         * gps.setTitle(R.string.menu_gps_off); }
         */
    } else if (mCurrentState == STATE_ROCK_EDIT) {
        menu.clear();
        inflater.inflate(R.menu.rock_edit_menu, menu);
        MenuItem undoButton = menu.findItem(R.id.rock_undo_move);
        if (undoMoves.isEmpty()) {
            undoButton.setVisible(false);
        } else {
            undoButton.setVisible(true);
        }
    }
    return true;
}

From source file:de.dreier.mytargets.features.training.input.InputActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_grouping_none:
        targetView.setAggregationStrategy(EAggregationStrategy.NONE);
        break;/*from ww  w .  ja  v  a 2  s .  c o  m*/
    case R.id.action_grouping_average:
        targetView.setAggregationStrategy(EAggregationStrategy.AVERAGE);
        break;
    case R.id.action_grouping_cluster:
        targetView.setAggregationStrategy(EAggregationStrategy.CLUSTER);
        break;
    case R.id.action_show_end:
        setShotShowScope(ETrainingScope.END);
        break;
    case R.id.action_show_round:
        setShotShowScope(ETrainingScope.ROUND);
        break;
    case R.id.action_show_training:
        setShotShowScope(ETrainingScope.TRAINING);
        break;
    case R.id.action_keyboard:
        final EInputMethod inputMethod = targetView.getInputMode() == EInputMethod.KEYBOARD
                ? EInputMethod.PLOTTING
                : EInputMethod.KEYBOARD;
        targetView.setInputMethod(inputMethod, true);
        SettingsManager.setInputMethod(inputMethod);
        item.setChecked(inputMethod == EInputMethod.KEYBOARD);
        supportInvalidateOptionsMenu();
        return true;
    case R.id.action_timer:
        SettingsManager.setTimerEnabled(!SettingsManager.getTimerEnabled());
        openTimer();
        item.setChecked(SettingsManager.getTimerEnabled());
        supportInvalidateOptionsMenu();
        return true;
    case R.id.action_new_round:
        EditRoundFragment.createIntent(data.training).withContext(this).start();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
    item.setChecked(true);
    return true;
}

From source file:at.ac.tuwien.detlef.activities.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;/*from   w w  w.j  a  v a2s  . c  o  m*/

    switch (item.getItemId()) {
    case R.id.licenses:
        intent = new Intent(this, LicensesActivity.class);
        startActivity(intent);
        break;
    case R.id.settings:
        intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
        break;
    case R.id.playlist:
        intent = new Intent(this, PlaylistActivity.class);
        startActivity(intent);
        break;
    case R.id.refresh:
        onRefreshPressed();
        break;
    case R.id.add_new_podcast:
        intent = new Intent(this, AddPodcastActivity.class);
        startActivityForResult(intent, PODCAST_ADD_REQUEST_CODE);
        break;
    case R.id.menu_show_only_new_episodes:
        item.setChecked(!item.isChecked());
        getEpisodeListFragment().setReadFilter(item.isChecked());
        // getEpisodeListFragment().set
        break;
    case R.id.sort:
        android.support.v4.app.DialogFragment dialog = new EpisodeListSortDialogFragment();
        dialog.show(getSupportFragmentManager(), "EpisodeListSortDialogFragment");
        break;
    default:
        break;
    }
    return true;
}