Home » Android » android – Replace the action bar's title with a spinner (drop down)

android – Replace the action bar's title with a spinner (drop down)

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to display a spinner in the same position where the action bar’s default title appear.
I followed the instruction of the similar SO case here ,
so I managed to eliminate the title but still the spinner’s position is not aligned to the left, as you can see from this screen-shot

apinner not aligned to the left

Here are the main definitions of my application to reproduce this case:

AndroidMenifest.xml:

<application
        android:label="app"            
        android:icon="@drawable/ic_launcher"
        android:theme="@style/Theme.AppCompat" > 
...
    <activity
        android:name="gm.activities.ViewAllActivity">            
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="gm.activities.MainActivity" />
    </activity>

menu_view_all.xml:

<menu 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"
    tools:context="gm.activities.ViewAllActivity">
    <item android:id="@+id/spinner"
        android:title="will be replaced anyway"
        app:showAsAction="ifRoom"
        app:actionViewClass="android.widget.Spinner"
        android:layout_gravity="left"
        android:gravity="left"/>
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

and the relevant activity:

public class ViewAllActivity extends ActionBarActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_all_activity);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
...
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_view_all, menu);
        MenuItem item = menu.findItem(R.id.spinner);
        Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
        spinner.setGravity(Gravity.LEFT);
        SpinnerAdapter adapter;
        spinner.setAdapter(ArrayAdapter.createFromResource(this,
                R.array.all_table_views, android.R.layout.simple_spinner_item));
        spinner.setOnItemSelectedListener(this); // set the listener, to perform actions based on item selection
        return true;
     }

So – Can I align the spinner to the left of the action bar and how?
Is it correct to use spinner inside the actionbar and to set it through the menu.xml file as I did?

How to&Answers:

Action Views in the menu are always going to align to the right. If you want your Spinner to align left, it would be better to set it as a custom View on the ActionBar using the setCustomView() method. A custom View will align to the left by default, and will take the place of the title if that is hidden. Please note that this requires that you call setDisplayShowCustomEnabled(true) on the ActionBar.