Reputation: 805
How can I change the font color of the selected item in a spinner?
I am able to change the background color of the selected item, the color of the dropdown item etc, but not the text color of selected item... how can I do that?
my code is: this is spinner i am using--:
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="32dip"
android:background="@drawable/mybg"
android:divider="@drawable/list_divider"
android:drawSelectorOnTop="true"
android:popupBackground="#D3D5D3"
android:prompt="@string/activityy_prompt"
/>
this is mybg.xml
<!-- <item android:drawable="@drawable/blue" android:state_pressed="false"/> -->
<!-- <item android:drawable="@drawable/back11"/> -->
<item android:drawable="@drawable/greenyellow1" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/greenyellow1" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/greenyellow1" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/greenyellow1" android:state_selected="true"/>
<item android:drawable="@drawable/back11"/>
using these i am not able to change the text color of selecetd item...
Upvotes: 43
Views: 96102
Reputation: 11083
drawable/mybg:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true">
<color android:color="@color/black" />
</item>
</layer-list>
This will change the selected item color in the popup.
Upvotes: 18
Reputation: 8242
using selector as text color .
create color_selector.xml in drawable like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
and in textview
<TextView
android:textColor="@drawable/color_selector"/>
Upvotes: 7
Reputation: 1864
Setting ((TextView) view).setTextColor(getResources().getColor(R.color.black));
inside onItemSelected(...)
listener method will work.
It sets color for your selected spinner text.
Upvotes: 4
Reputation: 1803
Define OnItemSelectedListener
like this:
private AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(0x00000000);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
and then Set OnItemSelectedListener
to spinner
like this:
spinner.setOnItemSelectedListener(listener);
Upvotes: 86
Reputation: 584
I know this is old question, but I had big problem on changing color of selected item in spinner in TabLayout and this really worked for me:
spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE); //change to your color
}
});
Upvotes: 2
Reputation: 1433
The solution that worked for me was to use a CheckedTextView
for the the drop down resource view and then change the color of the checked item using a color selector.
spinner_dropdown_item.xml
in the layout
folder:
<?xml version="1.0" encoding="utf-8"?>
<!-- A `CheckedTextView` allows the color of the text to be changed when it is selected (checked). -->
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_item_textview"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:maxLines="1"
android:ellipsize="end"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textSize="18sp"
android:textColor="@color/spinner_color_selector"
android:background="@color/spinner_background" />
spinner_color_selector
in the color
folder:
<?xml version="1.0" encoding="utf-8"?>
<!-- Highlight the selected (checked) item when the spinner is open. -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:color="@color/white" />
<item android:color="@color/blue_100" />
</selector>
spinner_dropdown_item.xml
must be set as the DropDownResourceView
for the Adapter
. In my case I am using a ResourceArrayAdapter
that pulls information from multiple sources.
// Setup a `MatrixCursor` for the static entries.
String[] matrixCursorColumnNames = {DatabaseHelper._ID, DatabaseHelper.NAME};
MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames);
matrixCursor.addRow(new Object[]{-2, getString(R.string.first_spinner_item)});
matrixCursor.addRow(new Object[]{-1, getString(R.string.second_spinner_item)});
// Get a `Cursor` with the list of additional items from the database.
Cursor cursor = DatabaseHelper.getCursor();
// Combine `matrixCursor` and `cursor`.
MergeCursor mergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});
// Create a `ResourceCursorAdapter` for the spinner with `this` context. `0` specifies no flags.;
ResourceCursorAdapter resourceCursorAdapter = new ResourceCursorAdapter(this, R.layout.spinner_item, mergeCursor, 0) {
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Get a handle for the spinner item `TextView`.
TextView spinnerItemTextView = (TextView) view.findViewById(R.id.spinner_item_textview);
// Set the `TextView` to display the name.
spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.NAME)));
}
};
// Set the `ResourceCursorAdapter` drop drown view resource.
resourceCursorAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
// Get a handle for the `Spinner`.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Set the adapter for the folder `Spinner`.
spinner.setAdapter(resourceCursorAdapter);
Because ResourceCursorAdapter
uses the same bindView
to populate the spinner when it is open and closed, the id of the TextView
in spinner_dropdown_item.xml
and spinner_item.xml
must be the same.
spinner_item.xml
in the layout
folder:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_item_textview"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:maxLines="1"
android:ellipsize="end"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="18sp"
android:textColor="@color/primaryTextColor" />
Upvotes: 0
Reputation: 22404
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
((TextView) adapterView.getChildAt(0)).setTextColor(Color.WHITE);
}
Upvotes: 0
Reputation: 2295
some of you that using MaterialBetterSpinner and Binding your Layouts, all the above won't help, try this, hope it helps you:
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final YourXMLBinding rowBinding = DataBindingUtil.inflate(inflater, R.layout.yourXML, parent,false);
rowBinding.tv1.setText(mMy.getValues().get(position));
if(position == mMy.getCurrentIndex()) {
rowBinding.tv1.setTypeface(Typer.set(getContext()).getFont(Font.ROBOTO_BOLD));//change font
rowBinding.tv1.setTextColor(ContextCompat.getColor(getContext(), R.color.yourColor));//change color
}
return rowBinding.getRoot();
}
}
yourXML is something like this:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/colorBackgroundStart">
<TextView
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_weight="0.7"
android:layout_height="30dp"
android:textColor="#fff"
android:textSize="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="8dp"/>
</layout>
create a spinner with this adapter and yourXML :
final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues());
final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext());
spinner.setAdapter(adapter);
Upvotes: 2
Reputation: 95
You don't need java code for background color change in Android 2.3v. Just add
android:background="#F0F8FF"
to your spinner in xml file.
Upvotes: -1
Reputation: 595
You can change the selected text color by adding OnItemSelectedListener
to the spinner
qtySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) view).setTextColor(Color.BLACK); //Change selected text color
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Upvotes: 19
Reputation: 171
try implementing onItemSelected in your OnItemSelectedListener for change the text color of spinner selected item
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
int index = adapterView.getSelectedItemPosition();
((TextView) spinner.getSelectedView()).setTextColor(getResources().getColor(R.color.Blue)); //<----
Upvotes: 17
Reputation: 156
If your spinner is using an ArrayAdapter, you can simply pass a custom layout for your spinner items. That layout can be a simple textView but with the android:textColor attribute.
MainActivity.java onCreate()
adapter = new ArrayAdapter<>(this, R.layout.spinner_custom_textcolor, data);
spinner.setAdapter(adapter);
spinner_custom_textcolor.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:textColor="@color/YOUR_COLOR_HERE"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
*Everything from the above layout except for android:textColor is a direct copy from android.R.layout.simple_spinner_item
Upvotes: 3
Reputation: 802
just use this line onItemSelected listner -
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,long arg3)
{
item = (String) parent.getItemAtPosition(arg2);
((TextView) parent.getChildAt(0)).setTextColor(0x00000000);
}
Upvotes: 4
Reputation: 9576
See my answer to a similar question here. My answer is similar to Priya's, except it properly sets the default selected item's text color as well (so there's no lag with the wrong color when waiting for the spinner to automatically select the default item, which occurs after the user interface is already on-screen).
Upvotes: -1