I make a spinner dropdown menu for some NavigationDrawer menu items. Why can't I get a spinner variable, do both methods return null?

NavigationView nav_draw = (NavigationView) findViewById(R.id.nav_view); Menu menu = nav_draw.getMenu(); MenuItem item = menu.findItem(R.id.nav_myflat); 1) Spinner spinner = (Spinner) item.getActionView(); 2) View myView = getLayoutInflater().inflate(R.layout.activity_main, null); View v = myView.findViewById(R.id.spinner); Spinner spinner = (Spinner) v; 

Here is my activity_main:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:elevation="4dp" android:minHeight="?attr/actionBarSize" android:paddingTop="@dimen/tool_bar_top_padding" android:transitionName="actionBar" /> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:openDrawer="start"> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:itemIconTint="@color/colorAccent" app:itemTextColor="@color/colorPrimaryDark" app:menu="@menu/main_menu" /> </android.support.v4.widget.DrawerLayout> </RelativeLayout> 

main_menu.xml:

  <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:id="@+id/group1" android:checkableBehavior="single"> <item android:id="@+id/nav_uprav" android:title="@string/drawer_item_uprav" > <menu> <item android:id="@+id/nav_news" android:title="@string/drawer_item_news" /> <item android:id="@+id/nav_obiav" android:title="@string/drawer_item_obiav" /> <item android:id="@+id/submenu3" android:title="SubMenu 3" /> </menu> </item> <item android:id="@+id/nav_myflat" android:title="@string/drawer_item_myflat" > app:actionLayout="@layout/spinner"/> <menu> <item android:id="@+id/nav_auth" android:title="@string/drawer_item_auth" /> <item android:id="@+id/nav_video" android:title="@string/drawer_item_video" /> <item android:id="@+id/submenu5" android:title="SubMenu 6" /> </menu> </item> </group> </menu> 

spinner.xml:

 <?xml version="1.0" encoding="utf-8"?> <Spinner xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:gravity="center_vertical" /> 
  • Will your spinner be in item or item ? In other words, do you need a list tree? How to google play? - Flippy
  • I need to click on the menu item to reveal more items. I thought that it was necessary to screw the spinner to the item to the one that should open. Maybe I'm doing it wrong? then how you need it) - Anna
  • the library that advised drugs_and_code would suit, I thought the spinner would also look like that - Anna
  • 2
    The spinner in the item will open above and not below the item. Therefore, this library is quite good, but I would use MaterialDrawer, this is the side menu and a list of all types of items in one library without any xml at all - Flippy
  • one
    Yes, yes) You can safely erase all code and menus in both xml and java and go to this lib> github.com/mikepenz/MaterialDrawer - Flippy

2 answers 2

For drop-down menu items I will try to use this library here.

She helped me with this task.

  • Thanks, yes a good library, if I don’t work with a spiner, then I will try. So much time already spent on him (sorry to throw - Anna

With the MaterialDrawer library you can do this:

Drop-down menu item "Categories" with sub-items "Games" and "Movies"

 PrimaryDrawerItem sub_category_games = new PrimaryDrawerItem() .withName("Игры") .withIcon(R.drawable.ic_games); PrimaryDrawerItem sub_category_films = new PrimaryDrawerItem() .withName("Фильмы") .withIcon(R.drawable.ic_films); ExpandableDrawerItem category_expandable_item = new ExpandableDrawerItem() .withName("Категории") .withSelectable(false) .withSubItems(sub_category_games, sub_category_films) .withIcon(R.drawable.ic_category); Drawer drawer = new DrawerBuilder() .withActivity(this) .withToolbar(toolbar) .addDrawerItem(category_expandable_item) .build();