Colour wheel colour picker

Added a colour wheel-style colour picker in the following way. Documentation here and code here. Copyright 2012 Lars Werkman

Created new activity and layout, into Build, Edit libraries and dependencies and added com.larswerkman.holocolorpicker as a library dependency.

In the layout added:

    <com.larswerkman.holocolorpicker.ColorPicker
        android:id="@+id/picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <com.larswerkman.holocolorpicker.SaturationBar
        android:id="@+id/saturationbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/picker"
        android:layout_alignParentEnd="true" />

    <com.larswerkman.holocolorpicker.ValueBar
        android:id="@+id/valuebar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/saturationbar"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:layout_below="@+id/valuebar">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Change Color"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text Color"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:layout_weight="1" />

    </LinearLayout>

In the activity implement ColorPicker.OnColorChangedListener and declare these:

    private ColorPicker picker;
    private SaturationBar saturationBar;
    private ValueBar valueBar;
    private Button button;
    private TextView text;

Then in onCreate

        picker = (ColorPicker) findViewById(R.id.picker);
        saturationBar = (SaturationBar) findViewById(R.id.saturationbar);
        valueBar = (ValueBar) findViewById(R.id.valuebar);
        button = (Button) findViewById(R.id.button1);
        text = (TextView) findViewById(R.id.textView1);
        picker.addSaturationBar(saturationBar);
        picker.addValueBar(valueBar);
        picker.setOnColorChangedListener(this);
        valueBar.setOnValueChangedListener(new ValueBar.OnValueChangedListener() {
            @Override
            public void onValueChanged(int value) {

            }
        });
        saturationBar.setOnSaturationChangedListener(new SaturationBar.OnSaturationChangedListener() {
            @Override
            public void onSaturationChanged(int saturation) {

            }
        });

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //try to get hex of colour
                text.setText(Integer.toHexString(picker.getColor()).substring(2));

                text.setTextColor(picker.getColor());
                picker.setOldCenterColor(picker.getColor());
            }
        });

Finally add

    @Override
    public void onColorChanged(int color) {
        //gives the color when it's changed.
    }

 

This entry was posted in Android. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *