Adding Spinner to Actionbar (Not Navigation

Adding spinner to ActionBar (not Navigation

Well, I ditched the Spinner idea for using a submenu. I realized that the spinner was for selecting things that stayed selected; submenus seamed to be a better UI fit.

Can I add spinners to ActionBar or ActionMode in android?

Yes you obama.

Action bar mainly contains four functional areas. They are app icon, view control, action buttons and action overflow.

App Icon – App branding logo or icon will be displayed here.

View Control – A dedicated space to display app title. Also provides option to switch between views by adding spinner or tabbed navigation.

Action Buttons – Some important actions of the app can be added here.
Action Overflow – All unimportant action will be shown as a menu.

Check out the following diagram for complete overview about action bar.

Sample Image

With the API 21 the method setNavigationMode(ActionBar.NAVIGATION_MODE_LIST) is deprecated.

The best way to work with a spinner is to use a Toolbar like this:

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_actionbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary">

<Spinner
android:id="@+id/spinner_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

You can find an example in the Google IO 2014

Add a spinner to the Toolbar for a specific Fragment only

  1. Do setHasOptionsMenu(true) in your Fragment which need to show the spinner.

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    }
  2. Add menu item.

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
    android:id="@+id/spinner"
    android:title="ActionBar Spinner"
    app:actionViewClass="android.widget.Spinner"
    android:background="#ff00"
    app:showAsAction="always" />
    </menu>
  3. Then inflate menu in onCreateOptionsMenu method, refer to this:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu,inflater);
    inflater.inflate(R.menu.spinner, menu);
    MenuItem item = menu.findItem(R.id.spinner);
    Spinner spinner = (Spinner) item.getActionView();
    //.....
    }

How can I add my spinner to the ActionBar?

Step #1: Get rid of your Spinner.

Step #2: Get rid of your OnItemSelectedListener.

Step #3: Provide your ArrayAdapter as the first parameter to setListNavigationCallbacks().

Step #4: Provide an implementation of ActionBar.OnNavigationListener as the second parameter to setListNavigationCallbacks().

Step #5: In the onNavigationItemSelected() callback method in your ActionBar.OnNavigationListener, do whatever it is you want to do based upon the change in the state of the navigation (e.g., execute a FragmentTransaction).

Step #6: Redesign your application to not start an activity based on this navigation selection, as you are attempting above. Either start the activity from a toolbar button or options menu item, or use fragments to replace (part of) the UI on the existing activity. List and tabs navigation in the action bar is not for launching activities.

Can I disable ActionBar's navigation Spinner?

Yes, it is possible to disable the Spinner used in list navigation in ActionBar. But is't not a straightforward solution, rather a hack. ActionBar doesn't provide a direct access to the Spinner view. Unfortunately the Spinner is created in a private code without any id.

So how to access the Spinner instance? One solution could be to access it via Java reflection API, but I wouldn't recommend that.

A better solution is to get the root view for the current Activity, traverse it's child views (action bar and all it's views are present in the view hierarchy) and find the proper Spinner. Since the Spinner in action bar is presumably the only one you haven't created yourself, you should be able to distinguish it from the others.

Getting the root View is described in this SO question.

The traversal is rather simple, just bear in mind that if you are using ActionBarSherlock, you have to look for IcsSpinner instead of Spinner (IcsSpinner does not extend from Spinner).

private View findActionBarSpinner() {
View rootView = findViewById(android.R.id.content).getRootView();
List<View> spinners = traverseViewChildren( (ViewGroup) rootView );
return findListNavigationSpinner(spinners); // implement according to your layouts
}

private List<View> traverseViewChildren(ViewGroup parent) {
List<View> spinners = new ArrayList<View>();
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
if (child instanceof Spinner) {
spinners.add( child );
} else if (child instanceof IcsSpinner) { // add this if you are using ActionBarSherlock
spinners.add( child );
} else if (child instanceof ViewGroup) {
spinners.addAll( traverseViewChildren( (ViewGroup) child ) );
}
}
return spinners;
}

The function findListNavigationSpinner should be implemented in a way that you are able to distinguish the other spinners. If you are not using any Spinner (or any view derived from it), the returned list should contain just one item.

The code above describes how to get the Spinner in an Activity. Naturally, it is not a problem to disable the Spinner from within a Fragment. The fragment has a reference to it's activity, so the activity can expose the code to the fragment via some interface.

Unable to add drop-down navigation in action bar

You should put your code here to check what's happening, but this code worked for my:

   SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.activity_array, android.R.layout.simple_spinner_dropdown_item);

ActionBar bar = getActionBar(); //or getSupportActionBar() depending on your target version

bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ActionBar.OnNavigationListener listener = new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {

return false;
}
};

bar.setListNavigationCallbacks(mSpinnerAdapter, listener);

TO make this working you have to take care of the action bar library that is being used. Here you can check which library you need and the code to get it. It also includes an example of navigation drop-down :
developer.android.com/intl/es/guide/topics/ui/actionbar.html



Related Topics



Leave a reply



Submit