Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full-width Spinner in ActionBar

I'd really like my app to have a Spinner which stretches the entire length of my ActionBar, like the one in Gmail 4.0. Anyone know how to achieve this? Even if I set "match_parent" in the Spinner layout resource, it doesn't fill the entire bar. Preferably, I'd like to be able to have it fill the entire bar except for my action items, rather than using the split actionbar as well.

EDIT: see my answer below for an implementation using the built-in actionbar, or hankystyles' when using a custom view

enter image description here

like image 909
Alex Curran Avatar asked Jan 20 '12 11:01

Alex Curran


2 Answers

Bit annoying that I've just done it, but here is a method of doing it using the built-in Spinner in the action bar. All you need to do is make your spinner item's main container a RelativeLayout and set its gravity to fillHorizontal, like so:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="fill_horizontal" android:orientation="vertical" >  <TextView     android:id="@android:id/text1"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_alignParentLeft="true"     android:layout_alignParentTop="true"     android:layout_marginBottom="-4dip"     android:text="@string/gen_placeholder"     android:textAppearance="?android:attr/textAppearanceMedium" />  <TextView     android:id="@+id/TextView1"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_alignParentLeft="true"     android:layout_below="@android:id/text1"     android:text="@string/app_name"     android:textAppearance="?android:attr/textAppearanceSmall" />  </RelativeLayout> 

And initialising the Adapter as so:

ArrayAdapter<CharSequence> barAdapter = new ArrayAdapter<CharSequence>(this, R.layout.subtitled_spinner_item,      android.R.id.text1, getResources().getStringArray(R.array.actionList)); barAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

which then gives the required spinner spanning the entire actionbar (except for the action buttons):

Spanned actionbar

like image 53
Alex Curran Avatar answered Sep 22 '22 13:09

Alex Curran


The spinner on my gmail app also spans the entire width of my action bar. This solution worked for me:

View spinner = getLayoutInflater().inflate(R.layout.actionbar_spinner, null); actionBar.setCustomView(spinner); actionBar.setDisplayShowCustomEnabled(true); 

Where the spinner layout is like so:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:gravity="fill_horizontal" >     <Spinner          android:id="@+id/spinner"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:prompt="@string/spinner_text"     /> </RelativeLayout> 

I suspect there's a better solution to be had by overriding the default action bar navigation style like this but I couldn't immediately get that working.

like image 42
hankystyles Avatar answered Sep 20 '22 13:09

hankystyles