Issue
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.
Solution
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);
Answered By - Avinash Kumar Pankaj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.