4

I want to create a spinner with white background & black border and arrow icon (without using image if possible).

But when I use the following code, the spinner displays white background with black border but does not display arrow icon.

 <Spinner
    android:id="@+id/spinner"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1.5" 
    android:background="@drawable/txtbox_row_border"
    android:spinnerMode="dropdown"
     >
</Spinner>

txtbox_row_border.xml code

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" />
<stroke
    android:width="1dp"
    android:color="#515151" />
</shape>

And when i use the following code, the spinner displays arrow icon but with gray colored background.

 <Spinner
    android:id="@+id/spinner"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1.5" 
    android:background="@android:drawable/btn_dropdown" 
    android:spinnerMode="dropdown" >
</Spinner>

How can i achieve all my requirements in a single block of code? Thanks in advance.

Suniel
  • 1,449
  • 8
  • 27
  • 39
  • 2
    http://stackoverflow.com/questions/11188398/how-to-change-the-spinner-background-design-and-color-for-android – IntelliJ Amiya Sep 11 '15 at 05:55
  • This is the first thing i have implemented. When I use android:background="@color/white", then the arrow icon become invisible. – Suniel Sep 11 '15 at 06:02
  • 1
    you can find answer [here][1], both are same question. [1]: http://stackoverflow.com/questions/17231683/how-to-create-custom-spinner-like-border-around-the-spinner-with-down-triangle-o – Rahul Chaudhary Sep 11 '15 at 06:04
  • Thank you. it works after some customization. – Suniel Sep 11 '15 at 07:05

3 Answers3

1

You can change spinner border by pragmatically using create one method

 public static GradientDrawable backgroundWithBorder(int bgcolor,
            int brdcolor) {

        GradientDrawable gdDefault = new GradientDrawable();
        gdDefault.setColor(bgcolor);
        gdDefault.setStroke(2, brdcolor);
        gdDefault.setCornerRadii(new float[] { radius, radius, 0, 0, 0, 0,
                radius, radius });

        return gdDefault;

}

And then set by

act_delivery_address_type_spn.setBackground(Methods
                .backgroundWithBorder(getResources()
                        .getColor(R.color.txt_white),
                        getResources().getColor(R.color.border_gray)));

for background and border used this method. for arrow icon create custom spinner

Maulik Santoki
  • 532
  • 4
  • 14
  • This was a starting off point for me. This didn't work by itself, but when I got the selected view from the spinner, and set the background from there, it worked then. This was what I did: spinners[y].getSelectedView().setBackground(backgroundWithBorder(colorBackground, colorBorder)); – Peter Griffin Aug 13 '18 at 00:29
0

Try this...

Create a new Style is styles.xml

<style name="spinner_style">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:background">@drawable/border</item>
        <item name="android:layout_margin">10dp</item>
        <item name="android:paddingLeft">8dp</item>
        <item name="android:paddingRight">8dp</item>
        <item name="android:paddingTop">5dp</item>
        <item name="android:paddingBottom">5dp</item>
        <item name="android:popupBackground">#DFFFFFFF</item>
    </style>

Create border shape in drawable border.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="1dp"
        android:color="#515151" />
</shape>

And spinner would be like

Apply the style to the Linear Layout

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/spinner_style">

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:spinnerMode="dropdown" >
        </Spinner>

    </LinearLayout>
Arnold Brown
  • 1,330
  • 13
  • 28
0

1 - Create a custom_spinner.xml

2- Set a TextView within this "custom_spinner.xml" and config all that you want.

<?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:singleLine="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:soundEffectsEnabled="true"
        android:background="#000000"
        android:ellipsize="marquee"
        android:textColor="@color/textWhite"

        android:drawableRight="@drawable/ic_keyboard_arrow_right_black_24dp"

        android:textAlignment="inherit"/>