Reputation: 85
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1F"
android:layout_alignTop="@+id/txtLabel1F"
android:entries="@array/cat_array"
android:prompt="@string/cat_promt"
android:textColor="#ffffff"
/>
My Background is black and the item showing is with gray bg and black font color. But when it come to the selected it shows the font as black so it is not being seen. How do I change the color in it?
Upvotes: 5
Views: 16439
Reputation: 1986
Simplest use this
use this to change the text of selected Text
YOUR_SPINNER.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
TextView selectedText= view.findViewById(R.id.text_view_name_in_Adapter);
selectedText.setTextColor(getResources().getColor(R.color.YOUR_COLOR));
}
}
Upvotes: 4
Reputation: 2295
If you are using MaterialBetterSpinner
and Binding your Layouts,
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();
}
}
Your XML should be 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"/>
</LinearLayout>
</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: 0
Reputation: 2933
You need to create a custom spinner layout to achieve what you want.
check out these questions, they have the answers you want:
How to customize a Spinner in Android
Android: Custom Spinner Layout
The idea is to create a layout for your row, and set it when creating a spinner with its adapter in code.
Upvotes: 5