Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Spinner dropdown icon

The solutions I found to change the spinner dropdown icon where all:

1. create a custom drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/vector_drawable_ic_dropdown_black" android:state_focused="true" android:state_pressed="false" />
    <item android:drawable="@drawable/vector_drawable_ic_dropdown_black" android:state_focused="true" android:state_pressed="true" />
    <item android:drawable="@drawable/vector_drawable_ic_dropdown_black" android:state_focused="false" android:state_pressed="true" />
    <item android:drawable="@drawable/vector_drawable_ic_dropdown_black" />
</selector>

2. Set the drawable as the spinner background:

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:background="@drawable/custom_spinner_icon"
    android:gravity="center"
    android:paddingBottom="8dp"
    android:paddingTop="8dp"
    android:textColor="@color/textcolorprimary" />

And the result is:

enter image description here

As you can see this is not an acceptable solution since the icon needs to be right aligned and not stretched.

What can I do to make the icon not stretch and align it right?

EDIT

Since there are no working solutions yet I guess I have to specify my question. This is how my Spinner looks using the standard theme:

<Spinner
    android:id="@+id/products_download_spinner_language"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:gravity="center"
    android:paddingBottom="8dp"
    android:paddingTop="8dp"
    android:textColor="@color/textcolorprimary"
    android:theme="@android:style/Theme.Holo.Light.DarkActionBar" />

enter image description here

And everything I want (it is really not much I guess) is changing the arrow. I don't want that arrow in the right bottom corner to be displayed, I want this arrow to be displayed vertically centered at the right:

enter image description here

And every solution which i tried until now:

Spinner Dropdown Arrow

How to set dropdown arrow in spinner?

simply weren't working. They had stretched icons or the bottom line was missing or something else went totally wrong. I just want another arrow.

like image 481
Mulgard Avatar asked May 24 '16 11:05

Mulgard


3 Answers

Try applying following style to your spinner using

style="@style/SpinnerTheme"

//Spinner Style:

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

//bg_spinner.xml Replace the arrow_down_gray with your arrow

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>

        <layer-list>

            <item>
                <shape>
                    <gradient android:angle="90" android:endColor="#ffffff" android:startColor="#ffffff" android:type="linear" />

                    <stroke android:width="0.33dp" android:color="#0fb1fa" />

                    <corners android:radius="0dp" />

                    <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
                </shape>
            </item>

            <item android:right="5dp">

                <bitmap android:gravity="center_vertical|right" android:src="@drawable/arrow_down_gray" />

            </item>

        </layer-list>

    </item>

</selector>
like image 161
PEHLAJ Avatar answered Oct 24 '22 07:10

PEHLAJ


Add theme to spinner

<Spinner style="@style/SpinnerTheme"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

Add spinnerTheme to styles.xml

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

Add New -> "Vector Asset" to drawable with e.g. "ic_keyboard_arrow_down_24dp"

Add spinner_background.xml to drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <item android:drawable="@drawable/ic_keyboard_arrow_down_24dp" android:gravity="center_vertical|right" android:right="5dp"/>
        </layer-list>
    </item>
</selector>
like image 41
Moshood Awari Avatar answered Oct 24 '22 07:10

Moshood Awari


For this you can use .9 Patch Image and just simply set it in to background.

android:background="@drawable/spin"

Here i'll give you .9patch image. try with this.

enter image description here enter image description here

Right click on image and click Save Image as

set image name like this : anyname.9.png and hit save.

Enjoy.. Happy Coading. :)

like image 20
Ronak Selarka Avatar answered Oct 24 '22 06:10

Ronak Selarka