Question

I need my spinners to have two different textcolors (green = ok, red = not ok), depending on the "mastervalue".

For example:

If mastervalue = 1:

Spinner 1

  1. (green)
  2. (green)
  3. (green)
  4. (red)
  5. (red)

Spinner 2

  1. (green)
  2. (red)
  3. (red)
  4. (red)
  5. (red)

If mastervalue = 2:

Spinner 1

  1. (green)
  2. (green)
  3. (green)
  4. (green)
  5. (green)

Spinner 2

  1. (green)
  2. (green)
  3. (green)
  4. (green)
  5. (red)

So the colors have to change dynamically, every time I change the mastervalue. To check which values are green/red, I have a couple of many-to-many relationship tables in my db. I just have to find a way to set the text color for each value before/after populating a spinner.

Would be great to get some help here!

Was it helpful?

Solution 2

For your issue you simply need to create a simple custom SpinnerAdapter and manipulate it as you need:

so let's start:

1.First of all let's create a custom_spinner row (layout)

custom_spinner.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text_main_seen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

  1. Then you need to create a custom SpinnerAdapter (an ArrayAdapter)
    NB: You need here to modify the section start with the "coloration algorithm" to match exaclty what you are looking for.

MyAdapter.java

public class MyAdapter extends ArrayAdapter<String> {
        
        String[] spinnerValues;
        
        public MyAdapter(Context ctx, String[] objects) { 
            super(ctx, R.layout.custom_spinner, objects); 
            
            spinnerValues = objects;
        } 
        
        @Override public View getDropDownView(int position, View cnvtView, ViewGroup prnt) { 
            return getCustomView(position, cnvtView, prnt); 
        } 
        
        @Override public View getView(int pos, View cnvtView, ViewGroup prnt) { 
            return getCustomView(pos, cnvtView, prnt); 
        } 
        
        public View getCustomView(int position, View convertView, ViewGroup parent) { 
            LayoutInflater inflater = getLayoutInflater(); 
            
            View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false); 
            TextView main_text = (TextView) mySpinner .findViewById(R.id.text_main_seen); 
            
            //here you make the algorithm you want for coloration///////////////////////////
            if(spinnerValues[position].equals("blue")){
                mySpinner.setBackgroundColor(Color.BLUE);
                
            }else if(spinnerValues[position].equals("green")){
                mySpinner.setBackgroundColor(Color.GREEN);
                
            }else if(spinnerValues[position].equals("red")){
                mySpinner.setBackgroundColor(Color.RED);
            
            }else if(spinnerValues[position].equals("yellow")){
                mySpinner.setBackgroundColor(Color.YELLOW);
            }
            //end coloration algorithm//////////////////////////////////////////
            
            main_text.setText(spinnerValues[position]); 
            
            return mySpinner; 
        } 
    } 

And finally in your activity you refere to this Adapter in your spinner:

MainActivity.java

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //values to put in the spinner
        String[] values = { "blue", "red", "green", "yellow" };
        
        //my spinner
        Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
        
        //set MyAdaper
        mySpinner.setAdapter(new MyAdapter(this, values));

    }
}

At the end this is a simple test if it's needed ;) enter image description here


Good luck , just let me know if anything goes wrong ;)

OTHER TIPS

spinner_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="@dimen/button_height"
    />

MyParameter.java

public class MyParameter {
        public String value;
        public boolean allowed;

            public MyParameter(){
                super();
            }

        public MyParameter(final String value, final boolean allowed) {
            super();
            this.value = value;
            this.allowed = allowed;
        }
}

MyParameterAdapter.java

public class MyParameterAdapter extends ArrayAdapter<MyParameter> {

    Context ctx;
    MyParameter[] values;

    public MyParameterAdapter(Context ctx, MyParameter[] values) { 
        super(ctx, R.layout.li_spinner_item, values); 

        this.ctx = ctx;
        this.values = values;
    } 

    @Override public View getDropDownView(int position, View view, ViewGroup parent) { 
        return getCustomView(position, view, parent); 
    } 

    @Override public View getView(int pos, View view, ViewGroup parent) { 
        return getCustomView(pos, view, parent); 
    } 

    public View getCustomView(int position, View view, ViewGroup parent) { 

        View spinner = LayoutInflater.from(ctx).inflate(R.layout.spinner_item, parent, false); 
        TextView txtValue = (TextView) spinner.findViewById(android.R.id.text1); 

        if(values[position].allowed){
            spinner.setBackgroundColor(Color.GREEN);
        } else {
            spinner.setBackgroundColor(Color.RED);
        }

        txtValue.setText(values[position].value); 

        return spinner; 
    }
}

Before I populate the spinner I use the following function, to mark the values:

public static MyParameter[] markValues(final List<String> allValues, final List<String> matchingValues) {
        MyParameter[] values = new MyParameter[allValues.size()];

        for(int i=0; i<allValues.size(); i++){
            final MyParameter param = new MyParameter();
            param.value = allValues.get(i);
            param.allowed = matchingValues.contains(allValues.get(i));
            values[i] = param;
        }

        return values;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top