4

I have a rectangle box that I set the spinner background as but I would like to keep the down arrow that comes along with the spinner. Here is my code for the rectangle:

<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<stroke android:width="2dp" android:color="#80000000" />
<padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" />

See the picture below:

enter image description here

On the left is what i currently have, on the right is what I'm trying to achieve or at least have a down arrow in there somewhere. Any ideas on how to create this background?

Chandrahas Aroori
  • 955
  • 2
  • 14
  • 27
user112016322
  • 688
  • 3
  • 11
  • 26

3 Answers3

2

I checked your code with the help of uiautomator and the spinner dropdown arrow does not show up with the background set.

You could use a LayerList and have a bitmap with gravity right. Now the bitmap is part of the background set to spinner.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff"/>
    <stroke
        android:width="2dp"
        android:color="#80000000"/>
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp"/>

</shape>
</item>

    <item>
        <bitmap
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:src="@mipmap/ic_launcher" />
    </item>
</layer-list>

Result :

Of course there is no border around the bitmap. You can check this link How to change the spinner background design and color for android? and see if it fits your requirement

enter image description here

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Do you know how to change the size of the bitmap? its too big and setting the layout width and height to certain values doesn't do anything. – user112016322 Jan 25 '16 at 15:21
  • @user1197993 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html. check the docs see if that helps. – Raghunandan Jan 25 '16 at 16:20
0

Add Spinner inside a RelativeLayout. In my case, the icon disapper because of parent layout's height and width.

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:background="@drawable/startbg"
        android:layout_marginLeft="5dp"
        >
    <Spinner
        android:id="@+id/timerSpin"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:spinnerMode="dropdown"
        android:backgroundTint="@color/white"
        android:layout_centerInParent="true"
        android:textColor="@color/white"
        android:pointerIcon="arrow"
         />
    </RelativeLayout>
mahamrauf
  • 21
  • 4
-1

Check the below XML code to achieve what you need. It is similar to what Raghunandan posted / answered, but with some minor tweaks. Hope this will help you,

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

  <item>
    <layer-list>
      <item>
        <shape android:thickness="2dp">
          <gradient android:angle = "90" android:endColor = "#ffffff" android:startColor = "#ffffff" android:type = "linear"/>

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

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

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

      <item>
        <rotate
           android:fromDegrees = "90"
           android:pivotX = "85%"
           android:pivotY = "50%"
           android:toDegrees = "90">
          <shape
             android:shape = "line"
             android:thickness="2dp"
             android:top = "1dp">
            <stroke
               android:color="#80000000"
               android:width = "1dp"/>
          </shape>
        </rotate>
      </item>
      <item>
        <bitmap

           android:gravity = "center|right" android:src = "@drawable/abc_btn_check_to_on_mtrl_015"/>

      </item>
    </layer-list>
  </item>

</selector>

use the required drawable in the placeholder images i added in the code.

Hope this will help you,

Good Luck

Swaminathan V
  • 4,663
  • 2
  • 22
  • 32