Im trying to add a switch (checkbox as second option) to the navigation drawer. The "slide in menu". The default one you'll get when creating a new project with navigation drawer.
I've tried on a fresh new project so I dont mess up my 'real' project.
I tried this from SO
But without any luck. Cant seem to find anything else worth mentioning..
Im trying to add the switch at the last menuItem. activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Import"
android:checkable="true"/>
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Share" />
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="Send" />
<item
android:id="@+id/myswitch"
android:title=""
android:actionLayout="@layout/ttt"
/>
</menu>
</item>
</menu>
ttt.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Switch
android:id="@+id/ss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
The last item "id/myswitch" doens't show at all. The MainActivity.java is 100% default. Thats why I dont post it.
set DrawerLayout tools:openDrawer="end" set NavigationView android:layout_gravity="end" change tag view from androidx.
The user can view the navigation drawer when the user swipes a finger from the left edge of the activity. They can also find it from the home activity by tapping the app icon in the action bar. The drawer icon is displayed on all top-level destinations that use a DrawerLayout.
Instead of:
<item
android:id="@+id/myswitch"
android:title=""
android:actionLayout="@layout/ttt"
/>
write:
<item
android:id="@+id/myswitch"
android:title=""
app:actionLayout="@layout/ttt"
/>
Change android:actionLayout
to app:actionLayout
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With