Daltz333 / COSC-481W

0 stars 0 forks source link

Research on how to do a color picker in Android #5

Open Daltz333 opened 1 year ago

Daltz333 commented 1 year ago

We might not be able to find a canned solution, so one possible thought would be.

  1. Show image on page.
  2. Request user to tap where the grout is on the picture
  3. Grab the X/Y position of the tap, convert to coordinates on the image.

What needs to be done?

naveensureshh commented 1 year ago

import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import top.defaults.colorpicker.ColorPickerPopup;

public class MainActivity extends AppCompatActivity {

// text view variable to set the color for GFG text
private TextView gfgTextView;

// two buttons to open color picker dialog and one to
// set the color for GFG text
private Button mSetColorButton, mPickColorButton;

// view box to preview the selected color
private View mColorPreview;

// this is the default color of the preview box
private int mDefaultColor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // register the GFG text with appropriate ID
    gfgTextView = findViewById(R.id.gfg_heading);

    // register two of the buttons with their
    // appropriate IDs
    mPickColorButton = findViewById(R.id.pick_color_button);
    mSetColorButton = findViewById(R.id.set_color_button);

    // and also register the view which shows the
    // preview of the color chosen by the user
    mColorPreview = findViewById(R.id.preview_selected_color);

    // set the default color to 0 as it is black
    mDefaultColor = 0;

    // handling the Pick Color Button to open color
    // picker dialog
    mPickColorButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    new ColorPickerPopup.Builder(MainActivity.this).initialColor(
                            Color.RED) // set initial color
                            // of the color
                            // picker dialog
                            .enableBrightness(
                                    true) // enable color brightness
                            // slider or not
                            .enableAlpha(
                                    true) // enable color alpha
                            // changer on slider or
                            // not
                            .okTitle(
                                    "Choose") // this is top right
                            // Choose button
                            .cancelTitle(
                                    "Cancel") // this is top left
                            // Cancel button which
                            // closes the
                            .showIndicator(
                                    true) // this is the small box
                            // which shows the chosen
                            // color by user at the
                            // bottom of the cancel
                            // button
                            .showValue(
                                    true) // this is the value which
                            // shows the selected
                            // color hex code
                            // the above all values can be made
                            // false to disable them on the
                            // color picker dialog.
                            .build()
                            .show(
                                    v,
                                    new ColorPickerPopup.ColorPickerObserver() {
                                        @Override
                                        public void
                                        onColorPicked(int color) {
                                            // set the color
                                            // which is returned
                                            // by the color
                                            // picker
                                            mDefaultColor = color;

                                            // now as soon as
                                            // the dialog closes
                                            // set the preview
                                            // box to returned
                                            // color
                                            mColorPreview.setBackgroundColor(mDefaultColor);
                                        }
                                    });
                }
            });

    // handling the Set Color button to set the selected
    // color for the GFG text.
    mSetColorButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // now change the value of the GFG text
                    // as well.
                    gfgTextView.setTextColor(mDefaultColor);
                }
            });
}

}

naveensureshh commented 1 year ago

found this on geeks for geeks as a reference.