Search code examples
androidandroid-layoutandroid-actionbarandroid-toolbar

Android Toolbar - How to implement Spinner for navigation mode?


What is currently the correct way to implement the View Control (No. 2 from the below screenshot taken from Android's design guide): Action Bar design

I found this example but when I tried to replicate it, I noticed that methods like: actionBar.setNavigationMode() are already deprecated.

So how should I implement it? I thought at first that it's a Spinner but I see apparently that it's not exactly the same
and can I still use ActionBar or should I better move to use Toolbar (yes, I am confused...)


Solution

  • As you rightly said, the setNavigationMode() method is now considered passé. To get the spinner in API 21, you need to use the Toolbar in this way:

    <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" />
    
    </android.support.v7.widget.Toolbar
    

    Add the above code to your Activity's layout. To set up the Toolbar in this Activity, you need to do this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(toolbar);
    }
    

    Try this. This will work.