I understand that the closed spinner is actually a View
, I think. But I am guessing it has a TextView
there somewhere to show the text. How do I
I understand that the closed spinner is actually a View, I think.
Yes. Specifically, it is whatever you told your SpinnerAdapter
to create.
But I am guessing it has a TextView there somewhere to show the text.
That would depend on what you told your SpinnerAdapter
to create.
How do I get access to that TextView so I can change the textcolor?
Ideally, you don't -- you give it the right color in the first place, via whatever you told your SpinnerAdapter
to create. If the color varies, override getView()
in your SpinnerAdapter
and change the color at that point.
In a pinch, you can try calling getSelectedView()
to get the current View
being shown by the closed Spinner
, but whatever change you make here may be eliminated on the user's next selection, and the alternate color may return later on if the earlier View
gets recycled.
For Change Textcolor of closed Spinner I have do in this way it works
@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;
}
@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;
}
Just try this:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView)parent.getChildAt(0)).setTextColor(getResources().getColor(R.color.colorPrimary));
to do it programatically you have to extend the adapter class, something like:
ArrayAdapter<CharSequence> adapter = new ArrayAdater(this){
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
// change the color here of your v
v. ... etc. etc
}
}
To modify the text color create a new xml file in your res/layout folder (for example my_spinner_text.xml). Add a text view to the new xml file and modify how you want:
<TextView android:id="@+id/spinnerText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#CCCCCC"
android:textSize="20dp"
xmlns:android="http://schemas.android.com/apk/res/android"/>
Create an ArrayAdapter that uses the new TextView and set it to your spinner:
Spinner localSpinner = (Spinner)findViewById(R.id.mySpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_array,
R.layout.my_spinner_text);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
localSpinner.setAdapter(adapter);
You can change the text colour or can access the textview in setOnItemSelectedListener event,
spinnerObject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
((TextView)parentView.getChildAt(0)).setTextColor(Color.rgb(249, 249, 249));
}