How to add a spinner next to a menu in the toolbar
Asked Answered
I

2

12

I want my spinner to be next to my menu in the toolbar (to the left of the menu), but currently the spinner appear below the menu. Do i have to add it somehow inside the onCreateOptionsMenu(Menu menu)

enter image description here

My Activity:

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

 Spinner spinner = (Spinner) findViewById(R.id.travelType_spinner);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.travelType_array, R.layout.spinner_item);
        // 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
        if (spinner != null) {
            spinner.setAdapter(adapter);
        }

Layout:

<android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <Spinner
            android:id="@+id/travelType_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </android.support.design.widget.AppBarLayout>

Spinner_item.xml

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/whiteText"
    android:textStyle="bold"
    android:padding="15dp"/>
Invercargill answered 16/5, 2016 at 9:14 Comment(1)
Check your layout file once. Your spinner tag should be between the toolbar tag and not after it ends.Merilyn
W
46

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>

Java

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.android_action_bar_spinner_menu, menu);

        MenuItem item = menu.findItem(R.id.spinner);
        Spinner spinner = (Spinner) item.getActionView();

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.spinner_list_item_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);
        return true;
    }

or

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimaryDark"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
     >

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

</android.support.v7.widget.Toolbar>
Williamswilliamsburg answered 16/5, 2016 at 9:27 Comment(3)
and how can we add onItemSelectedListener for the spinner ?Duty
in the case of adding a custom list change the adapter part to List<customType> myList = //your data here; ArrayAdapter<customType> adapter= new ArrayAdapter<customType>(this , android.R.layout.simple_spinner_item , myList); Wig
hey, it only shows the spinner arrow for me, but the icon no longer shows upPurdah
B
0

Adding to the above answer, your activity needs to implement onItemSelectedListener, and in the onItemSelected method you do your work.

You will also need to tell the spinner who is listening, in onCreateOptionsMenu, add this line:

spinner.setOnItemSelectedListener(this);

which will cause your spinner to call the onItemSelected method of the activity.

Bartel answered 8/5, 2018 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.