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:com.recursionlabs.thecommuter.AboutFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_about, container, false);

    mDrawerLayout = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
    AboutActivity.mDrawerLayout = mDrawerLayout;

    navigationView = (NavigationView) rootView.findViewById(R.id.nav_view);
    navigationView.getMenu().getItem(3).setChecked(true);

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override/*from w w  w. jav  a  2  s .  c  om*/
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            menuItem.setChecked(true);
            int id = menuItem.getItemId();
            Intent intent;

            if (id == R.id.nav_favorites) {
                intent = new Intent(getActivity(), FavoritesActivity.class);
                startActivity(intent);
            } else if (id == R.id.nav_home) {
                intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
            } else if (id == R.id.nav_settings) {
                intent = new Intent(getActivity(), SettingsActivity.class);
                startActivity(intent);
            }

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

    TextView international = (TextView) rootView.findViewById(R.id.international_4_0);
    international.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("https://creativecommons.org/licenses/by-sa/4.0/deed.en"));
            startActivity(intent);
        }
    });

    TextView unported = (TextView) rootView.findViewById(R.id.unported_3_0);
    unported.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("https://creativecommons.org/licenses/by-sa/3.0/deed.en"));
            startActivity(intent);
        }
    });

    TextView generic_2_5 = (TextView) rootView.findViewById(R.id.generic_2_5);
    generic_2_5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("https://creativecommons.org/licenses/by-sa/2.5/deed.en"));
            startActivity(intent);
        }
    });

    TextView generic_2_0 = (TextView) rootView.findViewById(R.id.generic_2_0);
    generic_2_0.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("https://creativecommons.org/licenses/by-sa/2.0/deed.en"));
            startActivity(intent);
        }
    });

    TextView generic_1_0 = (TextView) rootView.findViewById(R.id.generic_1_0);
    generic_1_0.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("https://creativecommons.org/licenses/by-sa/1.0/deed.en"));
            startActivity(intent);
        }
    });

    Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
    if (toolbar != null) {
        toolbar.setTitle("About");
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_hamburger);
    }

    return rootView;
}

From source file:com.akshay.newmaterialdesignlibrary.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Adding Toolbar to Main screen
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);//  w w w .j  av a2s  . c o m
    // Setting ViewPager for each Tabs
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    // Set Tabs inside Toolbar
    TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
    tabs.setupWithViewPager(viewPager);
    // Create Navigation drawer and inlfate layout
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    // Adding menu icon to Toolbar
    ActionBar supportActionBar = getSupportActionBar();
    if (supportActionBar != null) {
        supportActionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
        supportActionBar.setDisplayHomeAsUpEnabled(true);
    }
    // Set behavior of Navigation drawer
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            // Set item in checked state
            menuItem.setChecked(true);

            // TODO: handle navigation

            // Closing drawer on item click
            mDrawerLayout.closeDrawers();
            return true;
        }
    });
    // Adding Floating Action Button to bottom right of main view
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(v, "Hello Snackbar!", Snackbar.LENGTH_LONG).show();
        }
    });
}

From source file:com.blackmoon.english.MainActivity.java

private void setupDrawerLayout() {
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    NavigationView view = (NavigationView) findViewById(R.id.navigation_view);
    view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override//w ww .  j  a va  2  s.  c  o  m
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            Snackbar.make(content, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
            menuItem.setChecked(true);
            drawerLayout.closeDrawers();
            return true;
        }
    });
}

From source file:com.dspot.declex.example.expenses.activity.MainActivity.java

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {

    if (prevCheckedItem != null)
        prevCheckedItem.setChecked(false);
    menuItem.setChecked(true);
    prevCheckedItem = menuItem;/*from w  w  w . jav  a 2s  . c o m*/

    switch (menuItem.getItemId()) {
    case R.id.nav_expenses_list:
        $ExpensesListFragment();
        break;

    case R.id.nav_expenses_per_week_list:
        $StatisticsFragment();
        break;

    case R.id.nav_new_expense:
        $ExpensesListFragment().addExpense(true);
        break;

    case R.id.nav_profile:
        $ProfileFragment();
        break;

    case R.id.nav_logout:
        $Logout();
        break;
    }

    drawer_layout.closeDrawer(GravityCompat.START);
    return true;
}

From source file:com.limakilogram.www.bawang.ui.main.MainActivity.java

private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override//  ww  w .java2  s. co m
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            menuItem.setChecked(true);
            mDrawerLayout.closeDrawers();

            Context context = getApplicationContext();

            switch (menuItem.getItemId()) {
            case R.id.nav_home:
                Intent intent1 = new Intent(context, OrderListActivity.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent1.putExtra(OrderListActivity.EXTRA_HISTORY, "order");

                context.startActivity(intent1);
                break;
            case R.id.nav_messages:

                Intent intent2 = new Intent(context, OrderListActivity.class);
                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent2.putExtra(OrderListActivity.EXTRA_HISTORY, "bid");
                context.startActivity(intent2);
                break;

            }
            return true;
        }
    });
}

From source file:com.karthikb351.vitinfo2.activity.MainActivity.java

private void initializeView() {

    navigationTabs = Arrays.asList(getResources().getStringArray(R.array.navigation_tab));

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mainContent = (LinearLayout) findViewById(R.id.llMainContent);
    headerCampus = (TextView) drawerLayout.findViewById(R.id.header_campus);
    headerUsername = (TextView) drawerLayout.findViewById(R.id.header_username);

    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);/*from  w w  w.  j a  v  a2s  .  com*/
    final ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.setDrawerListener(mDrawerToggle);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        mDrawerToggle.syncState();
    }

    headerUsername.setText(registerNumber);
    headerCampus.setText(Data.toTitleCase(campus));

    NavigationView view = (NavigationView) findViewById(R.id.navigation_view);
    view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(final MenuItem menuItem) {
            menuItem.setChecked(true);
            drawerLayout.closeDrawers();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    switchFragment(menuItem.getItemId());
                }
            }, Constants.DRAWER_CLOSE_TIME_OUT);
            return true;
        }
    });

    getSupportFragmentManager().beginTransaction()
            .add(R.id.flContent, new TodayFragment(), TodayFragment.class.getSimpleName())
            .commitAllowingStateLoss();
}

From source file:com.tnnowu.android.googlenavigation.BaseActivity.java

private void populateDrawerItems(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override// w  w w .j  a  v  a 2  s.  c  o  m
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            menuItem.setChecked(true);
            mItemToOpenWhenDrawerCloses = menuItem.getItemId();
            mDrawerLayout.closeDrawers();
            return true;
        }
    });
    if (FirstActivity.class.isAssignableFrom(getClass())) {
        navigationView.setCheckedItem(R.id.navigation_first);
    } else if (SecondActivity.class.isAssignableFrom(getClass())) {
        navigationView.setCheckedItem(R.id.navigation_second);
    } else if (ThirdActivity.class.isAssignableFrom(getClass())) {
        navigationView.setCheckedItem(R.id.navigation_third);
    }
}

From source file:com.example.mpodera.tourismapp.MainActivity.java

private void setNavigationDrawer() {
    mTitle = mDrawerTitle = getTitle();//from w  w w.ja  v a  2s  . c  om
    dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    navView = (NavigationView) findViewById(R.id.navigation);
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
            dLayout, /* DrawerLayout object */
            R.drawable.ic_menu_white_24dp, /* nav drawer image to replace 'Up' caret */
            R.string.abierto, /* "open drawer" description for accessibility */
            R.string.cerrado /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            if (navView.getMenu().findItem(menuItemD).isChecked()) {
                getSupportActionBar().setTitle(mTitle);
            } else {
                navView.getMenu().findItem(menuItemD).setChecked(false);
                getSupportActionBar().setTitle(R.string.aC);
            }
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            //getSupportActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    dLayout.setDrawerListener(mDrawerToggle);
    navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            if (menuItem.isChecked())
                menuItem.setChecked(false);
            else
                menuItem.setChecked(true);
            menuItemD = menuItem.getItemId();
            selectItem(menuItem.getItemId());
            setTitle(menuItem.getTitle());
            return false;
        }
    });
}

From source file:com.arta.lib.demo.widget.listviewanimations.itemmanipulationexamples.ExpandableListItemActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_expandable_limit:
        mLimited = !mLimited;//from  www . j a  v a 2s  .  c o  m
        item.setChecked(mLimited);
        mExpandableListItemAdapter.setLimit(mLimited ? 2 : 0);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

From source file:com.jsw.MngProductDatabase.Home_Activity.java

public void setupDrawerContent() {

    mMngPharmacy = null;//  w  ww .  j  ava  2 s  . co  m
    mManageProduct = null;

    mNav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            item.setChecked(true);

            switch (item.getItemId()) {
            case R.id.action_home:
                showHome();
                break;
            case R.id.action_list:
                showList();
                break;
            case R.id.pharmacy:
                showPharmacy();
                break;
            case R.id.action_sales:
                showSales();
                break;
            default:
                item.setChecked(false);
                break;
            }
            mDrawer.closeDrawer(GravityCompat.START);
            setTitle(item.getTitle());
            return true;
        }
    });
}