Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 773
GyRo Avatar asked Feb 22 '15 13:02

GyRo


People also ask

How do I replace my action bar toolbar?

To replace an app's default action bar with a Toolbar : Create a new custom theme and modify the app's properties so that it uses this new theme. Disable the windowActionBar attribute in the custom theme and enable the windowNoTitle attribute. Define a layout for the Toolbar .

What does action bar mean?

In Android applications, ActionBar is the element present at the top of the activity screen. It is a salient feature of a mobile application that has a consistent presence over all its activities. It provides a visual structure to the app and contains some of the frequently used elements for the users.


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

like image 150
Mike M. Avatar answered Oct 19 '22 01:10

Mike M.