Spinner with arrow in left
Asked Answered
P

7

16

I'm developing an app for RTL language and want to change the position of arrow to the left of Spinner ! Is there anyway to do this without creating a custom spinner ?

Presley answered 25/2, 2015 at 9:10 Comment(1)
Add a coment and explain why downVote ?Presley
T
15

You must write a custom spinner. Sample code is below. You can edit as you wish.

<Spinner
            style="@style/spinner_style"
            android:layout_width="match_parent"
            android:layout_height="wrap content"
             />

@style/spinner_style:

 <style name="spinner_style">
    <item name="android:background">@drawable/background_spinner</item>
</style>

@drawable/background_spinner

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item><layer-list>
        <item><shape>
               <solid android:color="@android:color/white" />
                <corners android:radius="4dp" />
                <padding android:left="8dp"/>
            </shape></item>
        <item><bitmap android:gravity="left"
              android:src="@drawable/ic_arrow_drop_down_grey600_36dp"/>                
        </item>
    </layer-list></item>
</selector>
Tm answered 25/2, 2015 at 10:36 Comment(0)
J
3

An easy and simple way to set arrow in left side is,You must create .xml file in drawable folder for that as written below:

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

   <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorWhite" />
        </shape>
   </item>
   <item
        android:drawable="@drawable/ic_arrow_drop_down_black_24dp"
        android:right="320dp"/>
</layer-list>

You can adjust above code as your need. And set this file as background of a spinner widget as below:

<Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layout_spinner"
        android:background="@drawable/arrow_left"
        android:elevation="5dp"/>
Jacaranda answered 20/3, 2017 at 5:40 Comment(0)
M
3

This worked in my case. I have English and Arabic languages in my app

<Spinner
    android:textDirection="locale"
    android:id="@+id/spnGender"
    style="@style/Base.Widget.AppCompat.Spinner.Underlined"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/array_gender"/>

This attribute is important.

android:textDirection="locale"

NOTE : I have used the default spinner without any customizations.

Manzanares answered 24/8, 2018 at 9:18 Comment(1)
This is the most correct solution. This one should be the accepted answer here.Helios
L
3
  • Create a .xml in drawable folder : @drawable/spinner_arrow.xml

<item>

    <layer-list>

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

                <stroke android:width="1dp" android:color="#ECF0F1" />

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

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

        <item         android:width="24dp"         android:height="24dp"         android:gravity="end|center_vertical"         android:drawable="@drawable/ic_keyboard_arrow_down_black_24dp"         />

    </layer-list>

</item>
- Then add it to the styles.xml
<style name="spinner_style">
    <item name="android:background">@drawable/spinner_arrow</item>
</style>
  • then add it to the spinner

    style="@style/spinner_style"

Lorenz answered 26/6, 2020 at 11:39 Comment(0)
L
2

dummy.xml (drawable should be of very low size-24dp)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list android:opacity="transparent">
            <item android:width="100dp" android:end="300dp">
                <bitmap android:src="@drawable/down_button_dummy_dummy" android:gravity="center"/>
            </item>
        </layer-list>
    </item>
</selector>

layout file snippet

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardUseCompatPadding="true"
        app:cardElevation="5dp"
        >
     <Spinner
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@drawable/dummy">

     </Spinner>
    </android.support.v7.widget.CardView>

Click here for rendered layout

Lowboy answered 23/11, 2017 at 12:0 Comment(0)
C
0

What about this ? First of all make sure that you enabled rtl support in AndroidManifest.xml

<application
...
android:supportsRtl="true"
...>

after that use this in your Spinner definition :

android:layoutDirection="rtl"
android:textDirection="rtl"
Chicle answered 9/11, 2019 at 21:49 Comment(0)
S
0

The easiest way: set in Spinner 'layoutDirection'= 'rtl' enter image description here

Superior answered 8/11, 2021 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.