Android Color Picker

Android Color Picker

try this open source projects that might help you

https://github.com/QuadFlask/colorpicker

How to create a simple custom color picker on android studio?

First thing first, you would need to use RadioButton for this use case. Since the Radio buttons have inbuild mechanism of selecting only one element at a time. You can read more about RadioButtons at https://developer.android.com/guide/topics/ui/controls/radiobutton .

In case you need select mutiple colors at a time, then use Checkbox. (Read more about checkbox at https://developer.android.com/guide/topics/ui/controls/checkbox)

Now, checkbox/radio buttons can take care of selection part, now we would need to make the UI updates based on whether the color is selected or not. For this use case, we can use StateDrawable, which can change it's UI based on the state (selected/unselected).

Following is an example of StateDrawable for checkbox/radio button:
File location: app/resources/drawables/color_picker_item.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/check_mark_image" android:state_checked="true"/>
<item android:drawable="@drawable/transparent" />
</selector>

The above drawable will show check_mark_image for checked state and transparent for unchecked state.

You can create this type of state drawable file for each color (this will allow to render different color checkbox based on requirement/color).

Or you can set the colors (blue, red, etc.) as background color to your checkbox and create only one state drawable file and reuse it for all based on your requirements.

You can assign this drawable to your checkbox as follow:

<CheckBox
...
android:button="@drawable/color_picker_item"/>

This way you can build your color picker.

How to create a Color picker like this image of color picker

For Simplicity you can use the existing library for color pickers, it's better not to reinvent the wheels and use the existing one.
Here is some top library for this.

Android Studio where is color picker for Flutter plugin

The color picker is not clickable in Android Studio running Flutter( Dart code), see picture below. But i found a work around using the Color class and manually opening color picker. Then pick a color and copy/paste it like this:

Color class

Here is how i do it:

1. Double tap shift to run search

Search icon

2. Type Color or Picker
Search list

3. Open Color Picker from the search list

Color Picker

4. Copy/paste the HEX color code into your color class.

Expert tip:

Add color picker to a keyboard shortcut. You can find the settings for Keymap, under File > Settings > Keymap
Sample Image

Android: colorPicker setButtonOkText not worked

Unfortunately the library looks like it has a bug.

The positive and negative buttons never have their text set to the passed in ids.

https://github.com/vadiole/colorpicker/blob/master/colorpicker/src/main/java/vadiole/colorpicker/ColorPickerView.kt

Look at lines 201 and 202 this is the only time these 2 buttons are pulled and neither of them call setText. The xml shows they are hardcoded to default strings.

Luckily there may be a workaround in here,

https://github.com/vadiole/colorpicker/blob/master/colorpicker/src/main/java/vadiole/colorpicker/ColorPickerDialog.kt

You will need to override two classes ColorPickerView and ColorPickerDialog

ColorPickerView will only need you to provide a modified constructor. Call it FixedColorPickerView

constructor(
context: Context,
actionOkRes: Int,
actionCancelRes: Int,
@ColorInt initialColor: Int = Color.DKGRAY,
colorModel: ColorModel,
colorModelSwitchEnabled: Boolean,
onSwitchColorModelListener: OnSwitchColorModelListener? = null,
) : super(context,
actionOkRes,
actionCancelRes,
initialColor,
colorModel,
colorModelSwitchEnabled,
onSwitchColorModelListener) {
//Your constructor will simply grab the buttons and set their texts
//after the real constructor has inited view.
val positiveButton = findViewById<Button>(R.id.positive_button)
val negativeButton = findViewById<Button>(R.id.negative_button)
positiveButton.setText(actionOkRes)
negativeButton.setText(actionCancelRes)
}

ColorPickerDialog you need to override 2 methods

onCreateDialog to replace theColorPickerView its creating.

onSaveInstanceState to avoid a NPE due to the reference to the old ColorPickerView

class FixedColorPickerDialog : ColorPickerDialog {
var fixPickerView: ColorPickerView by Delegates.notNull()

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val bundle = savedInstanceState ?: arguments!!
val actionOk = bundle.getInt(ACTION_OK_KEY)
val actionCancel = bundle.getInt(ACTION_CANCEL_KEY)

fixPickerView =
FixedColorPickerView(
requireContext(),
actionOk,
actionCancel,
bundle.getInt(INITIAL_COLOR_KEY),
ColorModel.fromName(bundle.getString(COLOR_MODEL_NAME_KEY)),
bundle.getBoolean(COLOR_MODEL_SWITCH_KEY),
onSwitchColorModelListener
)

fixPickerView.enableButtonBar(
object : ColorPickerView.ButtonBarListener {
override fun onNegativeButtonClick() = dismiss()
override fun onPositiveButtonClick(color: Int) {
onSelectColorListener?.onColorSelected(color)
dismiss()
}
}
)

return AlertDialog.Builder(requireContext()).setView(fixPickerView).create()
}
override fun onSaveInstanceState(outState: Bundle) {
val bundle =
makeArgs(
fixPickerView.actionOkRes,
fixPickerView.actionCancelRes,
fixPickerView.currentColor,
fixPickerView.colorModel,
fixPickerView.colorModelSwitchEnabled
)
outState.putAll(bundle)
super.onSaveInstanceState(outState)
}
}

Flutter: Circular Color Picker (Package: flutter_colorpicker)

please check out this and you need to palette type as paletteType: PaletteType.hueWheel,. use the same package as used.

import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';

void main() => runApp(const MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
const MyApp({Key key}) : super(key: key);

@override
State<StatefulWidget> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
bool lightTheme = true;
Color currentColor = Colors.amber;
List<Color> currentColors = [Colors.yellow, Colors.green];
List<Color> colorHistory = [];

void changeColor(Color color) => setState(() => currentColor = color);
void changeColors(List<Color> colors) => setState(() => currentColors = colors);

@override
Widget build(BuildContext context) {
final foregroundColor = useWhiteForeground(currentColor) ? Colors.white : Colors.black;
return AnimatedTheme(
data: lightTheme ? ThemeData.light() : ThemeData.dark(),
child: Builder(builder: (context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Color Picker Example'),
backgroundColor: currentColor,
foregroundColor: foregroundColor,

),
body: Container(
child: InkWell(
onTap: (){
showColorPicker();
},
child: Center(child: Text("Color Picker")),
),
),
);
}),
);
}

void showColorPicker() {
showDialog(context: context, builder: (BuildContext context){
return AlertDialog(
title: Text("Pick a color"),
content: SingleChildScrollView(
child: ColorPicker(
pickerColor: Color(0xff443a49),
paletteType: PaletteType.hueWheel,
),
),
);
});
}
}

output

Sample Image



Related Topics



Leave a reply



Submit