Skip to content

h6ah4i/ColorPicker

 
 

Repository files navigation

Color Picker

API License Maven Central

Yet another open source color picker for Android. So, why should you use this color picker? It is highly customizable and easy to use. You can simply add the ColorPreference to your preferences and a beautiful color picker dialog will be displayed without additional code. The color picker supports alpha and allows you to set your own presets.

The original ColorPickerView was written by Daniel Nilsson.

This project is a forked version of the Jared Rummler's ColorPicker library. This library uses AndroidX Preference Library instead of the native Android's Preference classes.

Screenshots

GIF    Screenshot

Usage

Add the ColorPreference to your preference XML:

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

  <PreferenceCategory>

    <com.jaredrummler.android.colorpicker.ColorPreference
        app:key="default_color"
        app:summary="@string/color_default_summary"
        app:title="@string/color_default_title"
        app:defaultValue="@color/color_default"/>

    ...

  </PreferenceCategory>

</PreferenceScreen>

Implement onDisplayPreferenceDialog() in your preference fragment:

public class MyPreferenceFragment extends PreferenceFragmentCompat {

  private static final String DIALOG_FRAGMENT_TAG =
          "androidx.preference.PreferenceFragment.DIALOG";

  @Override
  public void onDisplayPreferenceDialog(Preference preference) {
    // check if dialog is already showing
    if (getParentFragmentManager().findFragmentByTag(DIALOG_FRAGMENT_TAG) != null) {
      return;
    }

    if (preference instanceof ColorPreference) {
      final DialogFragment f = ((ColorPreference) preference).createDialog();
      f.setTargetFragment(this, 0);
      f.show(getParentFragmentManager(), DIALOG_FRAGMENT_TAG);
    } else {
      super.onDisplayPreferenceDialog(preference);
    }
  }
}

You can add attributes to customize the ColorPreference:

name type documentation
cpv_dialogType enum "custom" to show the color picker, "preset" to show pre-defined colors
cpv_showAlphaSlider boolean Show a slider for changing the alpha of a color (adding transparency)
cpv_colorShape enum "square" or "circle" for the shape of the color preview
cpv_colorPresets reference An int-array of pre-defined colors to show in the dialog
cpv_dialogTitle reference The string resource id for the dialog title. By default the title is "Select a Color"
cpv_showColorShades boolean true to show different shades of the selected color
cpv_allowPresets boolean true to add a button to toggle to the custom color picker
cpv_allowCustom boolean true to add a button to toggle to the presets color picker

For further documentation about how to use the library, check the demo app included in this project.

Download

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Step 2. Add the dependency

dependencies {
  compile 'com.github.h6ah4i:ColorPicker:develop-SNAPSHOT'
}

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

A highly customizable color picker for Android

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%