Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add switch button in navigation drawer

I want to use switch button in navigation drawer for adding and removing fragment from main layout.

this my code- menuitem.xml`

<group
    android:id="@+id/drawer_group1"
    android:checkableBehavior="single">
    <item
        android:id="@+id/nav_timer"
        android:icon="@drawable/ic_timer"
        android:title="Timer">

    </item>

    <item
        android:id="@+id/addFragment_Bt"
        app:actionViewClass="android.widget.Switch"
        android:title="Most Used" />
    <item
        android:id="@+id/nav_settings"
        android:icon="@drawable/ic_settings"
        android:title="Settings">

    </item>
</group>`

MainActivity.class

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        switch (menuItem.getItemId()) {
            case R.id.addFragment_Bt:

                Switch switchCompat = findViewById(R.id.addMostUsed_Bt);
                switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        FragmentManager fragmentManager = getSupportFragmentManager();
                        Fragment fragment = fragmentManager.findFragmentById(R.id.Most_Used_Fragment_container);
                        if (isChecked == true) {
                            if (fragment != null) {
                                fragmentManager.popBackStack();
                            }

                        }
                    }
                });
                break;
        }
    }
    mDrawerLayout.closeDrawer(GravityCompat.START);
    return true;

    }
}

for now i am just trying to remove already added fragment.

like image 394
neo Avatar asked Mar 04 '23 20:03

neo


2 Answers

menuitem.xml

<item android:id="@+id/nav_switch"
            app:actionLayout="@layout/switch_menu"
            android:title="Send"
            android:icon="@drawable/ic_menu_send"/>

switch_menu switch_menu is layout for switch.

switch_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_id"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:text=""/>
</LinearLayout>

Access Switch into activity:--

SwitchCompat switch_id;

switch_id =  actionView.findViewById(R.id.switch_id);
        switch_id.setChecked(true);
        switch_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), switch_id.isChecked()? "is checked!!!" : "not checked!!!",Toast.LENGTH_SHORT).show();
            }
        });

The output using above code is:

enter image description here

I hope its work for you.

like image 106
Android Geek Avatar answered Mar 10 '23 09:03

Android Geek


This Works for me.

///This is menu item.

<item
     android:id="@+id/darkModeMenu"
     android:title="Dark Mode"
     android:icon="@drawable/ic_darkmode"
     app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
     ></item>

//write this on the on create() method of Activity.

val menuItem = navigationView.menu.findItem(R.id.darkModeMenu)
    val switch_id = menuItem.actionView as SwitchCompat
    switch_id.setChecked(true)
    switch_id.setOnClickListener(View.OnClickListener {
        Toast.makeText(
            applicationContext,
            if (switch_id.isChecked()) "is checked!!!" else "not checked!!!",
            Toast.LENGTH_SHORT
        ).show()
    })
like image 44
Hussnain Hashmi Avatar answered Mar 10 '23 11:03

Hussnain Hashmi