Android dropdown color picker

I want to create a drop-down color set, something like this (sorry for the ugly image):

color dropdown picker

I only need some colors (say 6), so I don’t need a full set of colors, the drop-down menu will work fine.

I know that I need to expand the array adapter for Spinner and override getDropDownView and getView .

I do not know how to create a square frame with a border and a solid background color.

I know that I can define my own form inside drawable. In any case, I need to set the background color at runtime, so I also need to change the view and set the correct background color.

What is the best way to do this? Thanks.

+2
source share
1 answer

If you want only the background color, you can use this example.

public class CustomSpinnerAdapter<T extends BaseEntity> extends ArrayAdapter implements SpinnerAdapter { private final List<T> objects; // android.graphics.Color list public CustomSpinnerAdapter(Context context, List<T> objects) { super(context, R.layout.yourLayout, objects); this.context = context; this.objects = objects; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { super.getDropDownView(position, convertView, parent); View rowView = convertView; if (rowView == null) { // Get a new instance of the row layout view LayoutInflater inflater = this.activity.getLayoutInflater(); rowView = inflater.inflate(R.layout.yourLayout, null); rowView.setBackgroundColor(objects.get(position)); } else { rowView.setBackgroundColor(objects.get(position)); } return rowView; } @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; if (rowView == null) { // Get a new instance of the row layout view LayoutInflater inflater = this.activity.getLayoutInflater(); rowView = inflater.inflate(R.layout.yourLayout, null); rowView.setBackgroundColor(objects.get(position)); } else { rowView.setBackgroundColor(objects.get(position)); } return rowView; } } 
+5
source

All Articles