Reputation: 3096
I have added a Spinner with a custom listadapter which uses this layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:textColor="@drawable/default_spinner_item_text"
android:text="Sample Text"
android:textSize="20dp"
android:paddingBottom="7.5dp"
android:background="@drawable/default_spinner_item"
android:paddingTop="7.5dp">
</TextView>
Drawable default_spinner_item:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/default_button_text_color" />
<item android:state_checked="true" android:drawable="@android:color/transparent"/>
<item android:state_single="true" android:drawable="@android:color/transparent"/>
<item android:state_active="true" android:drawable="@android:color/transparent"/>
<item android:state_first="true" android:drawable="@android:color/transparent"/>
<item android:state_focused="true" android:drawable="@android:color/transparent" />
<item android:state_selected="true" android:drawable="@android:color/transparent" />
<item android:drawable="@color/default_button_border_color" />
</selector>
The spinner allways gets the same color as the dropdown items...
I have tried all the states but it seams impossible to make the spinner item background transparent and the dropdown list colored (default_button_border_color).
@Solved
This is my Adapter:
public class CustomSpinnerAdapter extends ArrayAdapter<String> {
private int _resource;
private Typeface _typeFace;
public CustomSpinnerAdapter(MainActivity context, int resource, List<String> items) {
super(context, resource, items);
_resource = resource;
_typeFace = context.getDefaultAppTypeFace();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView)super.getView(position, convertView, parent);
if(convertView == null)
view.setTypeface(_typeFace);
}
return view;
}
@Override
public TextView getDropDownView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getDropDownView(position, convertView, parent);
if(convertView == null)
view.setTypeface(_typeFace);
}
return view;
}
}
I now use two layouts, one for the view and one for the dropdownview. Another error in my code was that I called super.getView(position, convertView, parent); in the getDropDownView ovverride.
Upvotes: 2
Views: 2076
Reputation: 11
Use XML file to change the spinner color
<Spinner
android:id="@+id/idSpinner"
android:layout_width="match_parent"
android:layout_height="@dimen/_50sdp"
android:background="@color/white" />
Use Java file to change spinner color programmatically
idSpinner = findViewById(R.id.idSpinner);
idSpinner.setBackgroundColor(getResources().getColor(R.color.black));
Upvotes: 0
Reputation: 863
Try this
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/myspinner_background" />
under Drawable folder place this myspinner_background.xml
myspinner_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/big_drop_down_selected"
android:state_focused="false"
android:state_pressed="true"/>
<item android:drawable="@drawable/big_drop_down"/>
</selector>
in Activity
private ArrayAdapter<String> adpFromStation;
Spinner spnrFromStation = (Spinner) findViewById(R.id.spinner1);
adpFromStation = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,StationNamesList);
adpFromStation.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnrFromStation.setAdapter(adpFromStation);
Upvotes: 0
Reputation: 10047
You will have to do it from code.
The BaseAdapter
, ArrayAdapter
and others implement an interface called SpinnerAdapter
This interface provides a method called getDropDownView
. Using this method for the DropDown elements and the "normal" getView
method for the selection should do the trick.
If you're using ArrayAdapter
, you can also use it's setDropDownViewResource
method
Upvotes: 0
Reputation: 1720
just make two layout with desired color and set it like following:
ArrayAdapter<yourObjecttype> adapter = new ArrayAdapter<yourObjecttype(this,R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(R.layout.dropdown_layout);
Upvotes: 2