Prabha Karan
Prabha Karan

Reputation: 1319

How to add the button in a navigation Header in a navigation view?

I created the navigation drawer in a main activity.I have added a switch in a navigation header in a navigation layout.I have created a separate layout for the navigation header.I don't know how to add the functionality for the switch.How can I make the switch on and off for doing any functions.

  <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_dash_board"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.seyali.calllogs.DashBoardActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <include
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout="@layout/toolbar_layout"
            >

        </include>
    </LinearLayout>


    <android.support.design.widget.NavigationView

        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/navigation_View"
        android:layout_gravity="start"
        app:menu = "@menu/drawer_menu"
        app:headerLayout="@layout/navigation_drawer_header">
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

The below code is for navigation header layout.I added switch and the textview in a navigation header.navigation_drawer_header.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@drawable/backgroung_red_colour">
    <Switch
        android:text="Call Recording"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/navigation_drawer_switch"
        android:layout_alignParentBottom="true"
        android:gravity="left"
        android:textOff="off"
        android:textOn="on"/>

    <TextView
        android:text="Switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/navigation_drawer_switch"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="28dp"
        android:id="@+id/drawertext" />
</RelativeLayout>

Upvotes: 0

Views: 3630

Answers (2)

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this code:

 NavigationView navigationView = (NavigationView) findViewById(R.id.your_nav_view_id);
View header = navigationView.getHeaderView(0);

Switch switch_view = (Switch)header.findViewById(R.id.navigation_drawer_switch); 
switch_view.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    Log.v("Switch State=", ""+isChecked); 
    if(isChecked)
        //switch is on
    else
        //switch is off
    }       

});

Upvotes: 2

Adarsh
Adarsh

Reputation: 2269

View header = navigationView.getHeaderView(0);
Switch switch = (Switch)navigationView..findViewById(R.id.navigation_drawer_switch);

and use the 'switch' as a normal switch. hope this is what you looking for

Upvotes: 1

Related Questions