1

I want to change the check status of Switch button when the item (with a switch button inside) in the nav drawer is click. I dont know why my code doesnt work. Sorry for my bad english. Thank you in advance

This is the code for Navigation Drawer (MainActivity)

    navigationView = (NavigationView) findViewById(R.id.nav_view);

    Menu menu = navigationView.getMenu();
    MenuItem menuItem = menu.findItem(R.id.nav_switch);
    final View actionView = MenuItemCompat.getActionView(menuItem);
    actionView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mySwitch = (SwitchCompat) findViewById(R.id.switchbutton);
            mySwitch.setChecked(true);


        }
    });

    navigationView.setNavigationItemSelectedListener(this);

and this is the code for nav_drawer.xml menu

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_menu_home"
        android:title="Home" />
    <item
        android:id="@+id/nav_favorites"
        android:icon="@drawable/ic_menu_fave"
        android:title="Favorites" />
    <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_menu_manage"
        android:title="Tools" />

    <item android:id="@+id/nav_switch"
        app:actionLayout="@layout/action_view_switch"
        android:title="Offline Mode" />
</group>

This is the layout of item with switch button

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tetsujin.zumbadancetutorial.MainActivity">

   <android.support.v7.widget.SwitchCompat
    android:id="@+id/switchbutton"
    android:clickable="false"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    />
</LinearLayout>
devRey
  • 53
  • 1
  • 8
  • android:clickable="false" change to true. in SwitchCompat – DKV Sep 19 '16 at 04:50
  • did u get the answer? – DKV Sep 19 '16 at 05:24
  • Please go through following link, i think you get your solution. http://stackoverflow.com/a/31233478/1343788 – Mehul Kabaria Sep 19 '16 at 06:23
  • @mehul-kabaria Specifically I want to make the Twitter night mode thing. [Twitter night mode][1] [1]: https://tctechcrunch2011.files.wordpress.com/2016/07/night-mode-twitter.png?w=738 like this – devRey Sep 20 '16 at 02:06
  • @v-v It didn't work. I disabled the clickable of SwitchCompat because i want If i clicked the whole item with the switch button is clicked, I want to setChecked the switch button. Please read the comment above.Thank you – devRey Sep 20 '16 at 02:08
  • What i want to do is when the item with the switch is clicked in the nav drawer. I want to setCheck the switch button even if I didn't click the switch. – devRey Sep 20 '16 at 02:10

0 Answers0