How to Add a Dropdown Item on the Action Bar

How to add a Dropdown item on the action bar

First option:

menu/options.xml:


<item
android:icon="@drawable/ic_menu_sort"
android:showAsAction="ifRoom">
<menu>
<item
android:id="@+id/menuSortNewest"
android:title="Sort by newest" />
<item
android:id="@+id/menuSortRating"
android:title="Sort by rating" />
</menu>
</item>

Second option:

menu/options.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menuSort"
android:showAsAction="ifRoom"
android:actionLayout="@layout/action_sort" />
</menu>

layout/action_sort.xml:

<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_menu_refresh"
android:entries="@array/order" />

Docs for menu resources - http://developer.android.com/guide/topics/resources/menu-resource.html

Adding a drop-down item in the action bar

You can create a simple adapter to use with spinner:

ArrayAdapter.createFromResource(
this,
R.array.planets_array,
android.R.layout.simple_spinner_item
).also { adapter ->
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// Apply the adapter to the spinner
spinner.adapter = adapter
}

Add this to res/values/arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>

I took that code from the example in this official documentation:

Spinner: https://developer.android.com/guide/topics/ui/controls/spinner

How to set active item in the Action Bar drop-down navigation?

I just found that function. It is setSelectedNavigationItem(int position).

Set the selected navigation item in list or tabbed navigation modes.

Example:

actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(adapter, this);
actionBar.setSelectedNavigationItem(position);

How can I create a dropdown options menu from my action bar item?

Per the Defining Menus via XML Guide:

You can add a submenu to an item in any menu (except a submenu) by adding a <menu> element as the child of an <item>. Submenus are useful when your application has a lot of functions that can be organized into topics, like items in a PC application's menu bar (File, Edit, View, etc.).

They give an example XML of:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/file"
android:title="@string/file" >
<!-- "file" submenu -->
<menu>
<item android:id="@+id/create_new"
android:title="@string/create_new" />
<item android:id="@+id/open"
android:title="@string/open" />
</menu>
</item>
</menu>

In this case, your onOptionsItemSelected would look for create_new and open actions (and the file item would be handled by the menu itself).

How to implement custom drop-down menu to actionbar in android

Regarding the action bar's structure, you have two options for the left side and one for the right side.

Left side:

Enable Action bar drop-down navigation

 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

or

Add a custom View as navigational element via ActionBar#setCustomView(), a Spinner in your case.

Right side:

Add only one MenuItem to the action bar as a custom Action View, a Spinner in your case.

How to add dropdown menu to ActionBar in Android project?

You can use something called an android spinner. It looks like this:

Sample Image

You can customize it in many ways to suit your apps design too.

Here's a great tutorial by Google on how to use these:

http://developer.android.com/guide/topics/ui/controls/spinner.html

If you want to add this to an action bar, you can do it via a spinner adapter as detailed here:
http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown

If you want to add icons to do certain actions, then you can see this:
http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems

If you want to do certain actions in the bar itself (like search in the google search app) then see this:
http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

If you want to add navigation tabs, then see this:
http://developer.android.com/guide/topics/ui/actionbar.html#Tabs



Related Topics



Leave a reply



Submit