I tried to set spinner with drop down arrow but i couldn\'t fix it can anyone help me with this? I have attached the source code.
my class file:
From the API level 16 and above, you can use following code to change the drop down icon in spinner. just goto onItemSelected in setonItemSelectedListener and change the drawable of textview selected like this.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// give the color which ever you want to give to spinner item in this line of code
//API Level 16 and above only.
((TextView)parent.getChildAt(position)).setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,ContextCompat.getDrawable(Activity.this,R.drawable.icon),null);
//Basically itis changing the drawable of textview, we have change the textview left drawable.
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
hope it will help somebody.
One simple way is to wrap your Spinner + Drop Down Arrow Image inside a Layout. Set the background of Spinner as transparent so that the default arrow icon gets hidden. Something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:orientation="horizontal">
<Spinner
android:id="@+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="4"
android:gravity="center"
android:background="@android:color/transparent"
android:spinnerMode="dropdown" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:onClick="showDropDown"
android:src="@drawable/ic_chevron_down_blue" />
</LinearLayout>
Here background.xml is a drawable to produce a box type background.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<corners android:radius="2dp" />
<stroke
android:width="1dp"
android:color="#BDBDBD" />
</shape>
The above code produces this type of a Spinner and icon.
As a follow-up to another answer, I was asked how I changed the spinner icon to get something like this:
One pretty easy way is to use a custom spinner item layout:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
R.layout.view_spinner_item,
ITEMS
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
In res/layout/view_spinner_item.xml
, define a TextView with android:drawableRight
pointing to the desired icon (along with any customisations to text size, paddings and so on, if you wish):
<?xml version="1.0" encoding="utf-8"?>
<!-- Custom spinner item layout -->
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="@dimen/text_size_medium"
android:drawablePadding="@dimen/spacing_medium"
android:drawableRight="@drawable/ic_arrow_down"
/>
(For the opened state, just use android.R.layout.simple_spinner_dropdown_item
or similarly create a customised layout if you want to tweak every aspect of your spinner.)
To get the background & colours looking nice, set the Spinner's android:background
and android:popupBackground
as shown in that other question. And if you were wondering about the custom font in the screenshot above, you'll need a custom SpinnerAdapter.
dummy.xml (drawable should be of very less size. i have taken 24dp)
<?xml version="1.0" encoding="utf-8"?>
<layer-list android:opacity="transparent" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="100dp" android:gravity="right" android:start="300dp">
<bitmap android:src="@drawable/down_button_dummy_dummy" android:gravity="center"/>
</item>
</layer-list>
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 to see resultant rendered layout