Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add my spinner to the ActionBar?

I'm trying to get my spinner working as the Action Bar Drop Down List item, but I cant seem to implement it at all, there aren't many tutorials for this after searching through Google. I think its something to do with the .setListNavigationCallbacks(); line of code, I just have no idea how to get this working from that line onwards.

// setup action bar for spinner
    ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    bar.setListNavigationCallbacks();

    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.tools_array_stopwatch, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    switch (arg2) {
    case 0:     
        break;

    case 1:                 
        Intent countdown = new Intent(this, CountdownActivity.class);
        startActivity(countdown);                       
        break;

    default :                       
        break;
    }
}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}
like image 342
Ben Reddicliffe Avatar asked Dec 28 '11 22:12

Ben Reddicliffe


People also ask

How do I add a spinner to my toolbar?

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.

How do you add action items to the action bar in android?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

What is the difference between ActionBar and toolbar?

The Toolbar is basically the advanced successor of the ActionBar. It is much more flexible and customizable in terms of appearance and functionality. Unlike ActionBar, its position is not hardcoded i.e., not at the top of an activity.

What is custom spinner in android?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. You can add a spinner to your layout with the Spinner object.


1 Answers

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.

like image 200
CommonsWare Avatar answered Sep 22 '22 15:09

CommonsWare