Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch in Android navigation drawer

Tags:

android

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.

like image 499
Frid Avatar asked Jan 28 '16 16:01

Frid


People also ask

How do I change my navigation drawer to open from right to left?

set DrawerLayout tools:openDrawer="end" set NavigationView android:layout_gravity="end" change tag view from androidx.

How do you use a navigation drawer?

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.


1 Answers

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.

like image 130
lenooh Avatar answered Oct 19 '22 17:10

lenooh