4

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...)

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
GyRo
  • 2,586
  • 5
  • 30
  • 38

1 Answers1

3

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.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • And this code should be part of the layout.xml of the relevant activity? – GyRo Feb 22 '15 at 14:15
  • The UI element that in my question's screen shot is not exactly Spinner - in spinner the arrow is in the middle of the text whereas in the view control it's opened by a triangle at the bottom-right corner. How do I create this element, in case it's not deprecated anymore... – GyRo Feb 22 '15 at 14:18
  • OK, and is Spinner shoud be the most recommended way to display different views in Activity, instead of View Control? – GyRo Feb 22 '15 at 14:41
  • What are you referring to as `View Control` ? There is no such widget in the Android framework. What you have shown in your question is a spinner ... – Yash Sampat Feb 22 '15 at 14:44
  • 1
    And to answer your question as to "what is most recommended way", the answer is always that **IT DEPENDS** – Yash Sampat Feb 22 '15 at 14:46
  • Thanks. It worked after I changed the style of the activity to 'Theme.AppCompat.Light.NoActionBar' otherwise I got an exception (Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead). In addition, I got confused because the Spinner's style in 'AppCompat' theme doesn't look like in the screenshot above, that is probably using 'Holo' Theme. Apparently it's not recommended to change the look of the spinner's button in different themes. (http://stackoverflow.com/questions/26924627/how-to-create-a-widget-holo-spinner-style-widget-in-v7) – GyRo Feb 22 '15 at 21:17