One way I found is to use setActionView on the right menu:
mNavigationView.getMenu().findItem(R.id.nav_connect) .setActionView(new Switch(this)); // To set whether switch is on/off use: ((Switch) mNavigationView.getMenu().findItem(R.id.nav_connect).getActionView()).setChecked(true);
You probably want the click listener to also change the state of the switch:
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.nav_connect: ((Switch) menuItem.getActionView()).toggle(); return true; } } }
I have not tried, but you could probably use android:actionLayout="@layout/switch_layout" in xml and point to the layout you created.
You can also try using ActionProvider , which can offer a little more reliability. I have not even tried this method.
source share