1

findViewById() works fine when you need to find something any UI element that is in your layout. However, using the second option of this answer How to add a Dropdown item on the action bar, I added a Spinner to my ActionBar.

Now the problem I am facing is how to retrieve a reference to this Spinner so that I can change the font that it uses. How do I do that?

My menu/sort_spinner looks like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/sort" 
        android:showAsAction="always"
        android:title="@string/sort"
        android:actionLayout="@layout/actionbar_spinner"/>
</menu>  

and layout/actionbar_spinner like this:

<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:spinnerMode="dropdown"
    android:id="@+id/spinner"
    android:entries="@array/type" />
Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221

2 Answers2

1

You have to do it in the public boolean onCreateOptionsMenu(Menu menu).

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.sort_spinner, menu);
    MenuItem spinnerItem = menu.findItem(R.id.sort);
    Spinner spinner = (Spinner)spinnerItem.getActionView().findViewById(R.id.spinner);
    //Your rest of code...
    return super.onCreateOptionsMenu(menu);
}
Harikrishnan
  • 7,765
  • 13
  • 62
  • 113
  • and the `Typeface`? I am not using a custom adapter :) – An SO User Jul 23 '14 at 08:47
  • are you sure about the `findItem(R.id.sort)`? Are you sure it shouldn't be `R.id.spinner`? – An SO User Jul 23 '14 at 08:49
  • You are assigning a MenuItem to a spinner. – Simas Jul 23 '14 at 08:51
  • @user3249477 Yeah, I had that nagging feeling that something isnt right :) – An SO User Jul 23 '14 at 08:51
  • 1
    Sorry, I didnt read the question fully and made a silly mistake in the code. I didn't try this out, but it should work. Updated the code. Please check. :) – Harikrishnan Jul 23 '14 at 08:53
  • @LittleChild Sorry, but as far as I know for changing the Typeface you will have to use custom adapter for the spinner. Check this answer: http://stackoverflow.com/questions/19150588/android-spinner-change-font-typeface/19150605#19150605 – Harikrishnan Jul 23 '14 at 09:05
1

Inside of your onCreateOptionsMenu you can save a global reference of your menu like this:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.actionbar_items, menu);
    mMenu = menu;
    ...
}

EDIT:

To change the Font you will however have to use an adapter. Here's an example that should work.

MenuItem item = mMenu.findItem(R.id.sort);
Spinner spinner = (Spinner) item.getActionView().findViewById(R.id.spinner);

MyAdapter adapter = new MyAdapter(this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

private class MyAdapter extends ArrayAdapter {

    public MyAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public TextView getView(int position, View convertView, ViewGroup parent) {
        TextView v = (TextView) super.getView(position, convertView, parent);
        v.setTypeface(myFont);
        return v;
    }

    public TextView getDropDownView(int position, View convertView, ViewGroup parent) {
        TextView v = (TextView) super.getView(position, convertView, parent);
        v.setTypeface(myFont);
        return v;
    }

}
Simas
  • 43,548
  • 10
  • 88
  • 116
  • and now for the `Typeface` for the text inside the spinner. How to? :) – An SO User Jul 23 '14 at 08:52
  • No way to avoid the adapter ? :) – An SO User Jul 23 '14 at 09:08
  • 1
    I am afraid not. You have to set the Typeface for the TextView and that cannot be done unless you make a custom adapter. – Harikrishnan Jul 23 '14 at 09:10
  • @LittleChild what do you need the spinner for, if you're not planning to use an adapter? How else would you populate it then? – Simas Jul 23 '14 at 09:14
  • @user3249477 I think he was actually speaking about custom adapter. If needed he can simply populate it using an `ArrayAdapter`. No need to customise. But for setting `TypeFace` he will definitely need to customise it. – Harikrishnan Jul 23 '14 at 09:29
  • @user3249477 using a string array straight from the XML using `android:entries="@array/something"` – An SO User Jul 23 '14 at 10:13
  • @LittleChild You do know that you can still use the xml array with the custom adapter? – Simas Jul 23 '14 at 10:17