Search code examples
androidandroid-spinnerandroiddesignsupport

How do I programatically access a spinner located inside the header of the NavigationView from the design support library?


I am using the new design support library. For the navigation view you can specify a header. What I'm trying to do is programatically access a spinner, which is located inside the nav_header xml.

Spinner spinner = (Spinner) findViewById(R.id.spinner); // Normal initialization of a spinner

Located in activity_main.xml

<android.support.design.widget.NavigationView
        android:id="@+id/navigation_left"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header" <-- declaring nav_header as header layout
        app:itemIconTint="@color/primary_text"
        app:itemTextColor="@color/primary_text"
        app:menu="@menu/nav_draw_items" />

nav_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/filter_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="#2364AA"
        android:elevation="4dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:navigationContentDescription="@string/abc_action_bar_up_description"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <LinearLayout
            android:id="@+id/spinner_ll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/spinner_front"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="8dp"
                android:layout_gravity="center_vertical"
                android:text="Sort By: "
                android:textColor="@android:color/white"
                android:textSize="16sp"/>

            <Spinner
                android:id="@+id/spinner"           <-- what I'm trying to access
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="true"
                android:spinnerMode="dropdown" />

        </LinearLayout>

    </android.support.v7.widget.Toolbar>

</LinearLayout>

Solution

  • Do something like this:-

    //Your navigation view
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_left);
    
    //Now get the Spinner from the navigationView
    Spinner spinner = (Spinner) navigationView.findViewById(R.id.spinner);