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:Main.java

public static void setMenuItemChecked(final Menu menu, final boolean checked, final int viewId) {
    final MenuItem v = menu.findItem(viewId);
    if (v != null) {
        v.setChecked(checked);
    }//from  w w w  .j a  v a2s. co m
}

From source file:Main.java

public static void setMenuItemChecked(final Menu menu, final boolean checked, final int viewId,
        final int checkedResId, final int uncheckedResId) {
    final MenuItem v = menu.findItem(viewId);
    if (v != null) {
        v.setChecked(checked);
        v.setIcon(checked ? checkedResId : uncheckedResId);
    }//w  w w.j a v  a 2 s.  co m
}

From source file:androidx.navigation.ui.NavigationUI.java

/**
 * Sets up a {@link BottomNavigationView} for use with a {@link NavController}. This will call
 * {@link #onNavDestinationSelected(MenuItem, NavController)} when a menu item is selected. The
 * selected item in the BottomNavigationView will automatically be updated when the destination
 * changes.//ww w.  j  ava  2s. c o  m
 *
 * @param bottomNavigationView The BottomNavigationView that should be kept in sync with
 *                             changes to the NavController.
 * @param navController The NavController that supplies the primary menu.
*                      Navigation actions on this NavController will be reflected in the
*                      selected item in the BottomNavigationView.
 */
public static void setupWithNavController(@NonNull final BottomNavigationView bottomNavigationView,
        @NonNull final NavController navController) {
    bottomNavigationView
            .setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    return onNavDestinationSelected(item, navController, true);
                }
            });
    navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
        @Override
        public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
            Menu menu = bottomNavigationView.getMenu();
            for (int h = 0, size = menu.size(); h < size; h++) {
                MenuItem item = menu.getItem(h);
                if (matchDestination(destination, item.getItemId())) {
                    item.setChecked(true);
                }
            }
        }
    });
}

From source file:com.audiokernel.euphonyrmt.MainMenuActivity.java

private static void setMenuChecked(final MenuItem item, final boolean checked) {
    item.setChecked(checked);
}

From source file:com.loopj.android.http.sample.Redirect302Sample.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.isCheckable()) {
        item.setChecked(!item.isChecked());
        if (item.getItemId() == 0) {
            enableRedirects = item.isChecked();
        } else if (item.getItemId() == 1) {
            enableRelativeRedirects = item.isChecked();
        } else if (item.getItemId() == 2) {
            enableCircularRedirects = item.isChecked();
        }//w w w . j  a  v  a2 s . co m
    }
    return super.onOptionsItemSelected(item);
}

From source file:com.commonsware.android.tablayout.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.fixed) {
        item.setChecked(!item.isChecked());

        if (item.isChecked()) {
            adapter.setPageCount(3);/*w  ww  .j a v a2  s  . co m*/
            tabs.setTabMode(TabLayout.MODE_FIXED);
        } else {
            adapter.setPageCount(10);
            tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
        }

        adapter.notifyDataSetChanged();

        if (pager.getCurrentItem() >= 3) {
            pager.setCurrentItem(2);
        }

        return (true);
    }

    return (super.onOptionsItemSelected(item));
}

From source file:es.sergiomartinez.basecleanarchitecture.modules.main.MainActivity.java

private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override/*from  w  w  w  .  j  a v a 2 s  .  co  m*/
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            menuItem.setChecked(true);
            drawerLayout.closeDrawers();
            return true;
        }
    });
}

From source file:es.sergiomartinez.basecleanarchitecture.modules.main.MainActivity.java

private void setupNavigationView() {
    if (navigationView != null) {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override//from   w  w  w.  ja  v  a  2  s . c  o m
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                menuItem.setChecked(true);
                drawerLayout.closeDrawers();
                return true;
            }
        });
    }
}

From source file:androidx.navigation.ui.NavigationUI.java

/**
 * Sets up a {@link NavigationView} for use with a {@link NavController}. This will call
 * {@link #onNavDestinationSelected(MenuItem, NavController)} when a menu item is selected.
 * The selected item in the NavigationView will automatically be updated when the destination
 * changes./*  ww  w. java 2s. c om*/
 *
 * @param navigationView The NavigationView that should be kept in sync with changes to the
 *                       NavController.
 * @param navController The NavController that supplies the primary and secondary menu.
*                      Navigation actions on this NavController will be reflected in the
*                      selected item in the NavigationView.
 */
public static void setupWithNavController(@NonNull final NavigationView navigationView,
        @NonNull final NavController navController) {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            boolean handled = onNavDestinationSelected(item, navController, true);
            if (handled) {
                ViewParent parent = navigationView.getParent();
                if (parent instanceof DrawerLayout) {
                    ((DrawerLayout) parent).closeDrawer(navigationView);
                }
            }
            return handled;
        }
    });
    navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
        @Override
        public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
            Menu menu = navigationView.getMenu();
            for (int h = 0, size = menu.size(); h < size; h++) {
                MenuItem item = menu.getItem(h);
                item.setChecked(matchDestination(destination, item.getItemId()));
            }
        }
    });
}

From source file:com.template.MainActivity.java

private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override//from   ww w  .j  a  v  a 2s  .c o m
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            menuItem.setChecked(true);
            mBinding.drawerLayout.closeDrawers();
            return true;
        }
    });
}