How to Add Spinner (Dropdown List) to Android ActionBar/Toolbar
https://www.viralandroid.com/2016/03/how-to-add-spinner-dropdown-list-to-android-actionbar-toolbar.html
ActionBar is also known as app bar, now it is also called toolbar. ActionBar is one of the most important UI elements in android application. We can add many things in android action bar like tabs, overflow/dropdown menu, app icon, navigation drawer, menu items icon, search box, spinner etc. In this tutorial, you will learn to implement spinner/dropdown menu to android actionbar. Adding spinner to app bar/ toolbar is very simple, you just need to create a XML file in res/menu/ folder and add a item like your over flow menu and spinner widget as item actionViewClass, rest in your java code.
Spinner can be added to android actionbar/toolbar with many ways. Here, I am going to show with one of the best and easiest ways.
Related:
Android Material Design Spinner DropDown Example
Android Custom Vertical Dropdown Icons Menu
Android Toolbar Example: How to Use Toolbar as ActionBar
First, you have to create an xml file in res/menu/ folder and add a menu item with id, name, actionViewClass. Menu item file will look like below.
res/menu/android_action_bar_spinner_menu.xml
Java Activity File
Open you java activity file, go to onCreateOptionMenu method if already have and if not then override new method called onCreateOptionMenu and link it to your spinner menu item file using getMenuInflater. And set adapter to the spinner menu item class. Java activity file will looks like below.
src/SpinnerAndroidActionBarToolBar.java
Styles.xml File
You can change spinner background color, text color from styles.xml file. Styles.xml file will look like this.
res/values/styles.xml
XML Layout File
Following is the default content of XML layout file.
res/layout/android_actionbar_toolbar_spinner_example_layout.xml
Now, run your Adding Spinner (Dropdown List) to Android ActionBar/Toolbar application and click on the dropdown arrow from actionbar, you will see spinner with list items.
Spinner can be added to android actionbar/toolbar with many ways. Here, I am going to show with one of the best and easiest ways.
Related:
Android Material Design Spinner DropDown Example
Android Custom Vertical Dropdown Icons Menu
Android Toolbar Example: How to Use Toolbar as ActionBar
Android Example: How to Implement Spinner in ActionBar/Toolbar
First, you have to create an xml file in res/menu/ folder and add a menu item with id, name, actionViewClass. Menu item file will look like below.
res/menu/android_action_bar_spinner_menu.xml
Java Activity File
Open you java activity file, go to onCreateOptionMenu method if already have and if not then override new method called onCreateOptionMenu and link it to your spinner menu item file using getMenuInflater. And set adapter to the spinner menu item class. Java activity file will looks like below.
src/SpinnerAndroidActionBarToolBar.java
Styles.xml File
You can change spinner background color, text color from styles.xml file. Styles.xml file will look like this.
res/values/styles.xml
XML Layout File
Following is the default content of XML layout file.
res/layout/android_actionbar_toolbar_spinner_example_layout.xml
Now, run your Adding Spinner (Dropdown List) to Android ActionBar/Toolbar application and click on the dropdown arrow from actionbar, you will see spinner with list items.