-1

I want to implement some EditText and Spinner, where I am using android:drawableRight on EditText but this is not property of spinner so what can I do where I used like this. I used android:background="@drawable/rounded_edittext" for rounded box. And android:entries is used for Gender's values.

This xml code I used

<EditText
   android:id="@+id/passsET"
   android:layout_width="350dp"
   android:layout_height="50dp"
   android:layout_below="@+id/emailET"
   android:layout_marginTop="10dp"
   android:paddingLeft="10dp"
   android:drawableRight="@drawable/passwords_icon"
   android:hint="Password"
   android:singleLine="true"
   android:textColor="@color/colorPrimaryDark"
   android:background="@drawable/rounded_edittext"
   android:textSize="16dp"
   android:layout_centerHorizontal="true"
   android:inputType="textWebPassword"/>
<Spinner
 android:id="@+id/spinGenders"
 android:layout_width="350dp"
 android:layout_height="40dp"
 android:layout_marginTop="10dp"
 style="@style/Base.Widget.AppCompat.Spinner.Underlined"
 android:entries="@array/Gender"
 android:textSize="15dp"
 android:gravity="center"
 android:background="@drawable/rounded_edittext"
 android:layout_below="@+id/spinProfilebys"
 android:textColorHint="@color/BLUE"
 android:layout_centerHorizontal="true">
</Spinner>
VKS GODIYAL
  • 57
  • 1
  • 9

4 Answers4

4

You can change background color and drop down icon like doing this way

Step1: In drawable folder make background.xml for border of spinner.

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<corners android:radius="5dp" />
<stroke
    android:width="1dp"
    android:color="@color/darkGray" />
</shape>  //edited

Step2: for layout design of spinner use this drop down icon or any image drop.pnjuse this image like as

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="3dp"
    android:layout_weight=".28"
    android:background="@drawable/spinner_border"
    android:orientation="horizontal">

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:layout_marginLeft="5dp"
        android:spinnerMode="dropdown" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:src="@mipmap/drop" />

</RelativeLayout>

Finally looks like below image and it is every where clickable in round area and no need of to write click Lister for imageView.

For more details , you can see Here

enter image description here

  • Without using ImageView we can not add image on spinner. – VKS GODIYAL Jul 19 '17 at 07:06
  • This is **quite odd**: `android:src="@mipmap/drop" />` The `mipmap` folders must contain the **launcher icon ONLY** - nothing else. It may work, but it's not adviceable. See this post: https://stackoverflow.com/a/28065664/2649012 – Phantômaxx Jul 19 '17 at 07:22
1

create a style like this

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

than apply to you spinner

<Spinner
 android:id="@+id/spinGenders"
 android:layout_width="350dp"
 android:layout_height="40dp"
 android:layout_marginTop="10dp"
 style="@style/SpinnerTheme"
</Spinner>

create a backgound like this in drawble folder

<?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>
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • It's not working I used one drawable file and one style file where I did this code but the result is same. – VKS GODIYAL Jul 19 '17 at 07:19
  • @VKSGODIYAL please accept my so it can help others olso – AskNilesh Aug 02 '17 at 11:56
  • I have one another problem with custom spinner, spinner item which is coming from server does not fit in spinner. I used layout gravity center but the item is shown at bottom position. – VKS GODIYAL Aug 03 '17 at 04:58
0

[In this image all the spinner item comes at middle but the Education Item comes at bottom position]

[1] First add Spinner in xml file

  <Spinner
         android:background="@drawable/spinner_back"
         android:id="@+id/fatherOccuption"
         android:layout_width="350dp"
         android:layout_height="50dp"
         android:textSize="18dp"
         android:layout_centerHorizontal="true"
         style="@style/Base.Widget.AppCompat.Spinner.Underlined"
         android:layout_marginTop="2dp"
         android:layout_below="@+id/fatherOcc"/>

Now I used this below code on drawable file

   <?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/spinner_img" />
        </item>
    </layer-list>
  </item>
</selector> 

And this is the adapter which is used for spinner item

 StringRequest stringIncome=new StringRequest(Request.Method.GET,OCC_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    String[] arr = response.split(",");
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>
                    (EditDetails5.this, android.R.layout.simple_list_item_1, 
                     arr);
                    fatherOccuption.setAdapter(adapter);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

    Toast.makeText(EditDetails5.this, error.toString(), 
Toast.LENGTH_LONG).show();
}
});
AppController.getInstance().addToRequestQueue(stringIncome);

[1]: https://i.stack.imgur.com/qzqgu.png
VKS GODIYAL
  • 57
  • 1
  • 9
0

Use FrameLayout to design spinner with arrow button. Check below sample code.

<FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/buttonbackground"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="@dimen/dimen_5"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="5dp"
                    android:orientation="horizontal">
                    <Spinner
                        android:id="@+id/genderEdt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#EEEEEE"
                        android:layout_gravity="center_vertical"
                        android:fontFamily="@string/font_family_regular"
                        android:layout_marginLeft="3dp"
                        android:layout_marginRight="3dp"
                        android:textSize="15sp"/>

                    <ImageView
                        android:layout_gravity="center_vertical|end"
                        android:foregroundGravity="center_vertical"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:src="@drawable/drop_down_arrow"
                        android:layout_marginRight="7dp"/>

                </FrameLayout>