Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change text color of IcsSpinner on ActionBarSherlock?

My Action bar is currently like this:

enter image description here

i want it to be like this:

enter image description here

ignore the black line beside the logo. Mainly what i want is to change the color of the text in the IcsSpinner to white

my activity code is:

    ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
            R.layout.sherlock_spinner_item, cities);
    listAdapter
            .setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
    setContentView(mViewPager);
    final ActionBar bar = getSupportActionBar();
    bar.setCustomView(R.layout.custom_actionbar);
    bar.setIcon(R.drawable.logo);
    bar.setDisplayShowCustomEnabled(true);
    IcsSpinner citySpinner = (IcsSpinner) (bar.getCustomView())
            .findViewById(R.id.city_spinner);
    citySpinner.setAdapter(listAdapter);

and my custom_actionbar.xml is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="right|center_vertical"
    android:orientation="horizontal" >

    <com.actionbarsherlock.internal.widget.IcsSpinner
        android:id="@+id/city_spinner"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

</RelativeLayout>

I even want the spinner to be just left to the refresh icon. Like in the second image.

Thank you

like image 541
Archie.bpgc Avatar asked Aug 22 '12 14:08

Archie.bpgc


2 Answers

You need to use getSupportActionBar().getThemedContext() as the Context instance given to the adapter for inflating layouts. This will use whatever theme is appropriate for inflating widgets inside the action bar rather than the theme set for the contents of your activity.

like image 183
Jake Wharton Avatar answered Sep 27 '22 19:09

Jake Wharton


Just to compliment the accepted answer: this worked for me after having some weird NullPointerException while trying to create new ArrayAdapter using constructor

    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    ArrayAdapter<CharSequence> adapter = 
        ArrayAdapter.createFromResource(actionBar.getThemedContext(),
        R.array.action_list, android.R.layout.simple_spinner_dropdown_item);
    actionBar.setListNavigationCallbacks(adapter, this);
    actionBar.setDisplayShowTitleEnabled(false);
like image 30
Bostone Avatar answered Sep 27 '22 18:09

Bostone