I want to create a dropdown color picker, something like this (sorry for the ugly image):
I only need some colors (let's say 6) so I don't need a complete color picker, the dropdown will work fine.
I know I have to extend the array adapter for the Spinner and override getDropDownView and getView.
The thing I don't know is how to create a square box with a border and a solid background color.
I know that I can define my own shape inside drawable. Anyway I have to set the background color at runtime so I also need to change the view and set the correct background color.
Which is the best way to do this? Thanks.
If you want to only background color you can use like 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;
}
}