1

I want to generate a spinner in Java for color selection, it should look like this when drops down:

enter image description here

My Java code now looks like this :

    final Spinner spinner = new Spinner(context);
    String[] colors = new String[]{"[1]", "[2]", "[3]", "[4]", "[5]", "[6]"};
    final List<String> colorsList = new ArrayList<>(Arrays.asList(colors));
    final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, colorsList) {
      @Override
      public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View view = super.getDropDownView(position, convertView, parent);
        TextView tv = (TextView) super.getDropDownView(position, convertView, parent);
        Color color = Color.rgb(18,18,218);
        switch(position){
           case 0:
              color = Color.rgb(28,158,218); 
              break;
           case 1:
              color = Color.rgb(218,58,218); 
              break;
           case 2:
              color = Color.rgb(128,18,28); 
              break;
        }
        return view;
      }
    };

But now no color is shown, all white. What would be a better way to do it?

Bonatti
  • 2,778
  • 5
  • 23
  • 42
Frank
  • 30,590
  • 58
  • 161
  • 244
  • 1
    Nice and clean approach will be custom adapter see https://stackoverflow.com/questions/35983176/how-to-create-spinner-list-using-customadapter-in-android. – Muneesh Jan 07 '20 at 17:33

2 Answers2

2

Instead of TextView set BackgroundColor to view. Beside this If you want to colorize the selected view, then you also have to override getView. Check below:

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

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

private View getCustomView(int position, View convertView, ViewGroup parent) {
    View view = super.getDropDownView(position, convertView, parent);

    if (position % 2 == 1) {
        view.setBackgroundColor(Color.parseColor("#FFC3C0AA"));
    }
    else {
        view.setBackgroundColor(Color.parseColor("#FFB5DCE8"));
    }

    return view;
}

Output:

enter image description here

Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
0

I did it like this :

  int itemColor[] = new int[]{Color.rgb(188,108,218),
                              Color.rgb(88,18,218),
                              Color.rgb(18,108,28),
                              Color.rgb(18,108,118),
                              Color.rgb(218,88,218),
                              Color.rgb(138,108,118)};

...

final Spinner spinner = new Spinner(context);
String[] colors = new String[]{"[1]", "[2]", "[3]", "[4]", "[5]", "[6]"};
final List<String> colorsList = new ArrayList<>(Arrays.asList(colors));
final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, colorsList)
{
  @Override
  public View getDropDownView(int position, View convertView, ViewGroup parent)
  {
    View view = super.getDropDownView(position, convertView, parent);
    TextView tv = (TextView) super.getDropDownView(position, convertView, parent);
    view.setBackgroundColor(itemColor[position]);
    return view;
  }
};

spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(spinnerArrayAdapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
  {
    ((TextView) parent.getChildAt(0)).setBackgroundColor(itemColor[position]);
  }

  @Override
  public void onNothingSelected(AdapterView<?> parent)
  {

  }
});
Frank
  • 30,590
  • 58
  • 161
  • 244