I saw many topics about how to change spinner\'s text color,but i couldnt understand how to use
spinner_item.xml
This is the simplest Method which I executed for spinner's text. I know its late but it will help some one.
select_gender.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
((TextView)parent.getChildAt(position)).setTextColor(Color.parseColor("#646b99"));
spinner_selected_gender=gender_list.get(position);
Toast.makeText(getApplicationContext(),"You selected"+spinner_selected_gender,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I have done this as following: I have used the getDropDownView()
and getView()
methods.
Use getDropDownView()
for opened Spinner
.
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
if (!((LabelItem) getItem(position)).isEnabled()) {
mTitle.setTextColor(activity.getResources().getColor(
R.color.context_item_disabled));
} else {
mTitle.setTextColor(activity.getResources().getColor(
R.color.context_item));
}
return view;
}
And use getView()
for closed Spinner
.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
mTitle.setTextColor(activity.getResources().getColor(
R.color.context_item_disabled));
return view;
}