0

I created a drawable for my spinner background. It should draw a border and put an arrow to the right side of the spinner. It works perfectly with API 21+ devices as you see below ;

enter image description here

But it doesn't work as i expected with API 21 and below devices. Here how spinner looks like API 21;

enter image description here

My spinner code;

val requesterSpinner = Spinner(context)
requesterSpinner.id = Spinner.generateViewId()
val requesterSpinnerParams = LayoutParams(300, LayoutParams.WRAP_CONTENT)
requesterSpinner.background = context.getDrawable(R.drawable.spinner_background)
requesterSpinner.layoutParams = requesterSpinnerParams

My drawable codes;

<?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="@color/white">
                </solid>

                <corners android:radius="3dp" />
                <stroke
                    android:width="1dp"
                    android:color="#4d4d4d"/>
            </shape>
        </item>
            <item android:drawable="@drawable/ic_baseline_arrow_drop_down_24"
                android:gravity="right"
                android:width="30dp">
            </item>
    </layer-list>
    </item>
</selector>

And i saw something like android:width is not working for API'S below 23. I am okay with that but as you see the gravity attribute is not working as well.

How can i align arrow image to the right below API 23 ?

1 Answers1

0

Spinner Code

<Spinner
    android:id="@+id/txt_unit"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_margin="1dp"
    android:background="@drawable/bg_spinner_down_arrow"
    android:drawableEnd="@drawable/ic_down_arrow_grey"
    android:gravity="center"
    android:paddingStart="@dimen/_5sdp"
    android:paddingEnd="@dimen/_5sdp"
    android:text="PCS" />

bg_spinner_down_arrow.xml

<?xml version="1.0" encoding="utf-8"?>
<selector 
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <layer-list>
        <item>
            <shape android:shape="rectangle">
                <solid
                    android:color="@color/background_secondary" />
                <corners
                    android:radius="@dimen/_4sdp"/>
            </shape>
        </item>
        <item android:gravity="center_vertical|end"
            android:end="@dimen/_2sdp"
            android:left="@dimen/_2sdp"
            android:drawable="@drawable/ic_down_arrow_grey">
        </item>
    </layer-list>
</item>

ic_down_arrow_grey.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16dp"
android:height="16dp"
android:viewportWidth="16"
android:viewportHeight="16">
<group>
    <clip-path android:pathData="M16,0l-0,16l-16,0l-0,-16z M 0,0"/>
    <path
        android:pathData="M3.5,6L8,10.5L12.5,6"
        android:strokeAlpha="0.8"
        android:strokeLineJoin="round"
        android:strokeWidth="1.5"
        android:fillColor="#00000000"
        android:strokeColor="@color/mono_primary"
        android:fillAlpha="0.8"
        android:strokeLineCap="round"/>
drjansari
  • 306
  • 1
  • 11
  • I am not creating spinner in xml, i am creating it programmatically so i can't use use drawableEnd. And as i mentioned that gravity attribute is not working for devices below API 23. – Tolgahan Tutar Mar 29 '21 at 07:23