SeslColorPickerDialogFragment
A DialogFragment that shows a color picker.
The basic SeslColorPickerDialogFragment is created using newInstance and the show method.
The newInstance constructor can be used to provide an initial color for the dialog.
The newInstance constructor can be used to provide an array of recently used colors.
The newInstance constructor can be used to provide an initial color, an array of recently used colors and whether to show the opacity bar.
The newInstance constructor can be used to provide an initial color, an array of recently used colors, whether to show the opacity bar, and whether to show only the spectrum view.
Note: You should pay attention to fragment lifecycle. When the activity is recreated (e.g. on configuration change), the fragment will also be recreated. In this case, you should use the setOnColorChangedListener method to set the listener again in the onCreate method of your activity.
If you want to use the eye dropper feature, your Activity must implement SeslColorPickerDialogFragment.OnBitmapSetListener. The onBitmapSet method will be called to get the Bitmap to be used for the eye dropper.
Example:
public class MyActivity extends AppCompatActivity implements SeslColorPickerDialogFragment.OnBitmapSetListener {
private SeslColorPickerDialogFragment mColorPickerDialogFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the color picker dialog fragment with a color set listener
mColorPickerDialogFragment = SeslColorPickerDialogFragment.newInstance(
new SeslColorPickerDialogFragment.OnColorSetListener() {
@Override
public void onColorSet(int color) {
// Handle the selected color
findViewById(R.id.colorPreview).setBackgroundColor(color);
}
},
Color.RED, // initial color
new int[] {Color.RED, Color.GREEN, Color.BLUE}, // recently used colors
true, // show opacity bar
false // show only spectrum
);
// Optionally set a color changed listener (e.g., for live preview)
mColorPickerDialogFragment.setOnColorChangedListener(new SeslColorPicker.OnColorChangedListener() {
@Override
public void onColorChanged(int color) {
// Live update preview
findViewById(R.id.colorPreview).setBackgroundColor(color);
}
});
// Show the dialog when needed, e.g., on button click
findViewById(R.id.showColorPickerButton).setOnClickListener(v -> {
mColorPickerDialogFragment.show(getSupportFragmentManager(), "color_picker");
});
}
// Implement the OnBitmapSetListener for the eye dropper feature
@NonNull
@Override
public Bitmap onBitmapSet() {
return BitmapFactory.decodeResource(getResources(), R.drawable.your_drawable)
}
}
Replace R.id.colorPreview
and R.id.showColorPickerButton
with actual view IDs from your layout. The onBitmapSet()
method should return the bitmap you want to use for the eye dropper feature. Remember to set the listeners again after configuration changes if needed, as described above.