Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android dropdown color picker

I want to create a dropdown color picker, something like this (sorry for the ugly image):

color dropdown picker

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.

like image 422
Antonio Avatar asked Dec 03 '25 11:12

Antonio


1 Answers

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;
    }
}
like image 76
KEYSAN Avatar answered Dec 06 '25 02:12

KEYSAN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!