Cristiano
Cristiano

Reputation: 3189

How to change spinner text and background color?

I put a spinner in AlertDialog and from some reason colors there are displayed differently than in normal activity. That brings me to this problem:

android spinner

When I have that spinner in normal activity, text color is black and background color of dropdown list is gray. Here is the opposite, background color of dropdown list is black and text color is white. That would also be OK but the problem is, as you can see on that image, that white text is almost invisible on that gray background.

I tried to define new TextView and apply new adapter but that affects only color of the dropdown list. After the item is selected, text is still white.

spinner_text.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView  
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"
  android:gravity="left"  
  android:textColor="@android:color/black"        
/>

Adapter

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_text, values);
spinner.setAdapter(adapter);

All I want is the same look like it would be if I have put a spinner in layout which is used by activity.

Upvotes: 0

Views: 10831

Answers (2)

Avinash Kumar Pankaj
Avinash Kumar Pankaj

Reputation: 1720

put these lines in your style

<style name="mytheme" parent="@android:style/Theme.Dialog">
    <item name="android:textColor">#000000</item>
</style>

and then in ur manifest file include this style like below

    <activity
        android:name="com.agencymodel.views.TempOrderActivity"          
        android:theme="@style/mytheme" >
    </activity>

Upvotes: 2

Pratik
Pratik

Reputation: 30855

As this because of you set the theme for your application. You need to implement your Custom adapter class and implement SpinnerAdapter for this.

here is the example for this

public class CusSpinnerAdapter extends ArrayAdapter<String> 
    implements SpinnerAdapter{
    private LayoutInflater inflate;
    private int resourceId;
    private String[] options;
    private int selIndex;
    private Context context;

    public CusSpinnerAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.inflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.resourceId = textViewResourceId;
        this.options = objects;
    }
    public void setSelectedIndex(int selIndex){
        this.selIndex = selIndex;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = inflate.inflate(resourceId, null);
            Holder holder = new Holder();
            holder.textView = (TextView)convertView.findViewById(R.id.spinner_item);
            convertView.setTag(holder);
        }
        Holder holder = (Holder)convertView.getTag();
        holder.textView.setText(options[position]);

        return convertView;
    }
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = inflate.inflate(resourceId, null);
            Holder holder = new Holder();
            holder.textView = (TextView)convertView.findViewById(R.id.spinner_item);
            convertView.setTag(holder);
        }
        Holder holder = (Holder)convertView.getTag();
        holder.textView.setText(options[position]);
        if(position==selIndex){
            holder.textView.setBackgroundColor(context.getResources().getColor(R.color.spinner_item_selected));
        }else
            holder.textView.setBackgroundColor(context.getResources().getColor(R.color.spinner_item_default));

        return convertView;
    }
    private class Holder{
        TextView textView;
    }
}

In this selIndex was selected index item. You need to implement this as you identify that which item was selected and set selected drawable for your item. Just implement on item selected of spinner control and from that set the index value for this adapter class.

Here is the another way also

https://stackoverflow.com/a/4662939/760489

Upvotes: 2

Related Questions