Matt
Matt

Reputation: 5511

Spinner with default icon + arrow

Assuming this is a Spinner, how do you set the default icon?

Currently, mine just shows the icon of the first item (bluetooth)

enter image description here

Upvotes: 3

Views: 3159

Answers (3)

jdowdell
jdowdell

Reputation: 1638

Wasted time on this today so I thought I'd provide the correct answer, even though this thread appears dead.

First, I had suspected as @Jon did that merely invoking the magical mystical ActionProvider would add the spinner triangle for us. It does not.

Second, it turns out Google is cheating and violating their own ActionBar icon guidelines to make this happen. If you look at http://androiddrawables.com/Menu.html you'll see that the share icon specifically has a spinner-like-triangle hacked into its corner.

Essentially, this means your leading assumption - that it is a Spinner - is false. It's just a button that was hacked to look like a spinner.

So, you can create your own custom icon in that vein if you want, just follow the usual instructions for specifying ActionBar button icons (http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems). If you have text buttons like I did, that's not going to work.

What I ended up doing involved making use of some spinner background nine patch files available in the support library, and the attribute 'actionBarItemBackground', added to the style applied to the buttons in question.

<item name="android:actionBarItemBackground" tools:ignore="NewApi">@drawable/abc_spinner_ab_default_holo_light</item>
<item name="actionBarItemBackground">@drawable/abc_spinner_ab_default_holo_light</item>

You'll want to keep in mind ActionBar with AppCompat actionBarItemBackground not working when attempting this hack.

Upvotes: 0

Jon
Jon

Reputation: 1850

I believe that specific example is a ShareActionProvider, which is a special sublcass of ActionProvider

If you are adding an ActionProvider in the xml for your menu you can treat it as a menu item and add the icon.

From ActionProvider documentation:

<item android:id="@+id/my_menu_item"
 android:title="Title"
 android:icon="@drawable/my_menu_item_icon"
 android:showAsAction="ifRoom"
 android:actionProviderClass="foo.bar.SomeActionProvider" />

Since I haven't used these I don't know enough to know if all ActionProviders have the spinner corner triangle... my guess is yes?

UPADTE:

I was just thinking about how SpinnerAdapter works and there is another way to do this. If you are using a custom subclass of SpinnerAdapter in your Spinner it will have 2 methods and you could do the following:

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
    {
    // return the view to display when you are looking at the dropdown,
    // so this will probably be a TextView and/or an ImageView
    }

@Override
public View getView(int position, View convertView, ViewGroup parent)
    {
    // This will show when the item at the provided position is 
    // selected. At this point you could return the ImageView you want to 
    // always appear at the top, such as the share icon.
    }

Upvotes: 4

Matt
Matt

Reputation: 5511

I "solved" this problem with a hack, which was putting an ImageView overtop the Spinner.

<Spinner
    android:layout_width="40dp"
    android:layout_height="40dp"
    />

<ImageView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:scaleType="fitCenter"
    android:src="@drawable/ic_share"
    android:adjustViewBounds="true"
    android:background="@color/same_as_parent_background"
    />

Upvotes: -2

Related Questions