Francisco Romero
Francisco Romero

Reputation: 13199

How can I replace the icon arrow of spinner in Android with an image?

I would like to change the default spinner icon arrow in Android with a custom image.

I made my custom spinner (based on this answer: How to customize a Spinner in Android, just changing the custom values) and it works well but I am not able to change the arrow of the spinner neither on styles or programatically.

I saw this question: How to change image of a spinner but as I do not have a Spinner tag it does not allow me to add the attribute android:spinnerSelector.

How can I replace the default arrow icon with an image?

Thanks in advance!

Upvotes: 2

Views: 17927

Answers (2)

Santa
Santa

Reputation: 485

You can directly change it in style,

<style name="CustomSpinnerTheme" parent="android:Widget.Spinner">
    <item name="android:background">@drawable/spinner_arrow</item>
</style>

Just use the drawable icon whichever you want and then set the theme in the spinner

<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/CustomSpinnerTheme"/>

Upvotes: 1

SGX
SGX

Reputation: 3353

Short example:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        this,
        R.layout.custom_spinner,
        ITEMS
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

Then create a custom xml (named:custom_spinner.xml) for spinner:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:textSize="yourTextsize"
    android:drawablePadding="paddingValueYouWant"
    android:drawableRight="@drawable/ic_arrow_down" <!--your custom icon-->
    />

For more customization spinner, follow this artircle: How to set font custom font to Spinner text programmatically?

Upvotes: 5

Related Questions