Replace the action bar title with a counter (dropdown)

I am trying to display the counter at the same position where the default title for the action bar is displayed. I followed the instructions of a similar SO application here , so I managed to exclude the name, but still the counter position is not aligned to the left, as you can see in this screenshot

apinner not aligned to the left

Here are the basic definitions of my application for reproducing 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 related activities:

 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 - Is it possible to align the counter to the left of the action bar and how? Is it right to use a spinner inside the action bar and set it via the menu.xml file, like me?

+8
android alignment android-actionbar android-ui android-actionbar-compat
source share
1 answer

The View action in the menu will always align to the right. If you want your Spinner align to the left, it would be better to set it as a custom View in the ActionBar using the setCustomView() method. The custom View will be left-aligned by default and will replace the title if it is hidden. Note that this requires that you call setDisplayShowCustomEnabled(true) on the ActionBar .

+5
source share

All Articles