Code Monkey home page Code Monkey logo

colorpicker's Introduction

ColorPicker Android Arsenal Build StatusDownload

A simple Android color picker library

screen1

screen2

screen3

Download

Download the latest JAR or grab via Gradle:

implementation 'petrov.kristiyan:colorpicker-library:1.1.10'

How to use

Example 1 :

ColorPicker colorPicker = new ColorPicker(activity);
colorPicker.show();
colorPicker.setOnChooseColorListener(new ColorPicker.OnChooseColorListener() {
	@Override
	public void onChooseColor(int position,int color) {
	    // put code
	}

	@Override
	public void onCancel(){
	// put code
	}
});

Example 2 :

final ColorPicker colorPicker = new ColorPicker(SampleActivity.this);
colorPicker.setFastChooser(new ColorPicker.OnFastChooseColorListener() {
	@Override
	public void setOnFastChooseColorListener(int position, int color) {
	  // put code
	}

	@Override
	public void onCancel(){
	// put code
	}
})
.setDefaultColor(Color.parseColor("#f84c44"))
.setColumns(5)
.show();

Example 3 :

final ColorPicker colorPicker = new ColorPicker(SampleActivity.this);
colorPicker.setOnChooseColorListener(new ColorPicker.OnChooseColorListener() {
	@Override
	public void onChooseColor(int position,int color) {
	// put code
	}

	@Override
	public void onCancel(){
	// put code
	}
})
.addListenerButton("newButton", new ColorPicker.OnButtonListener() {
	@Override
	public void onClick(View v, int position, int color) {
	    // put code
	}
})
.disableDefaultButtons(true)
.setDefaultColor(Color.parseColor("#f84c44"))
.setColumns(5)
.setDialogFullHeight()
.show();

What you can do

Choose your own colors

setColors(int resId); // using an array resource
setColors(ArrayList<String> colorsHexList); // using a list of hex colors
setColors(int... colorsList); // use a list of colors Color.RED,Color.Black etc
setDefaultColorButton(int color); // set the colorButton to check by default

Define Listeners

setOnFastChooseColorListener(OnFastChooseColorListener listener); // renamed in version 1.1.0
setOnChooseColorListener(OnChooseColorListener listener);

Add custom buttons

addListenerButton(String text, Button button, OnButtonListener listener); // custom button
addListenerButton(String text, final OnButtonListener listener); // it will generate a button with default style

General methods you can use:

setDialogFullHeight(); // bigger height
dismissDialog(); // dismiss dialog slowly
setColumns(int c); // set columns number
setTitle(String title); // set the title of the dialog
setTitlePadding(int left, int top, int right, int bottom);
disableDefaultButtons(boolean disableDefaultButtons); // use if you want to implement your own buttons

getDialogBaseLayout(); // returns the RelativeLayout used as base for the dialog
getDialogViewLayout(); // returns the view inflated into the dialog
getDialog(); // returns the dialog
getPositiveButton(); // returns the positive button defined by default
getNegativeButton(); // returns the negative button defined by default
setDismissOnButtonListenerClick(boolean dismiss); // renamed in version 1.1.0

ColorButtons changes you can do:

setColorButtonTickColor(int color);  // renamed in version 1.1.0
setColorButtonDrawable(int drawable);
setColorButtonSize(int width, int height);
setColorButtonMargin(int left, int top, int right, int bottom);
setRoundColorButton(boolean roundButton);

Removed from version 1.1.0

setPositiveButton(String text, OnButtonListener listener); // not present from version 1.1.0 
setNegativeButton(String text, OnButtonListener listener); // not present from version 1.1.0

Additional Credits

for the Material Dialog library for button design specs and implementation https://github.com/drakeet/MaterialDialog

License

The MIT License

Copyright (c) 2016 Petrov Kristiyan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

colorpicker's People

Contributors

affordall avatar kristiyanp avatar olliechick avatar ravidsrk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

colorpicker's Issues

Gradle build failed

I've tried to use implementation 'petrov.kristiyan:colorpicker-library:1.1.9'. So I've got gradle build fail.

Only works until implementation 'petrov.kristiyan:colorpicker-library:1.1.8'

Jcenter deprecation

Hello, thank you for your work.

I am using your library with jcenter() on my gradle. However when I build I see jcenter was deprecated and instead I'm using mavenCentral() for your 1.1.10 version but it says "Unable to resolve dependency"

I tried also 1.1.7 and 1.1.5 ( by using implementation 'petrov.kristiyan:colorpicker-library:1.1.5' )

and by adding

maven { url "https://dl.bintray.com/petrovkristiyan/maven" }

into repositories, but it didn't work when I delete jcenter. And as I read jcenter will be down on 2022-02-01

Context instead of Activity?

I'm calling the picker from an OnClickAdapter inside a RecyclerHolder, where I only have access to an unspecified Context. Right now, I'm casting Context to Activity, but that's a pretty ugly way to do things. I've taken a quick look at the code and it seems like Activity could be exchanged for the more generic Context. Would that be possible? Is there a reason for prefering Activity?

Firebase reporting Fatal Exception: java.lang.IllegalStateException

Fatal Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
       at android.view.ViewGroup.addViewInner(ViewGroup.java:4659)
       at android.view.ViewGroup.addView(ViewGroup.java:4495)
       at android.view.ViewGroup.addView(ViewGroup.java:4436)
       at android.view.ViewGroup.addView(ViewGroup.java:4409)
       at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:460)
       at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:99)
       at petrov.kristiyan.colorpicker.CustomDialog.onCreate(CustomDialog.java:20)
       at android.app.Dialog.dispatchOnCreate(Dialog.java:733)
       at android.app.Dialog.show(Dialog.java:469)
       at petrov.kristiyan.colorpicker.ColorPicker.show(ColorPicker.java:259)
       at com.warrina.sudan.activities.TextStatusActivity.lambda$onCreate$0(TextStatusActivity.java:90)
       at com.warrina.sudan.activities.TextStatusActivity.lambda$XnQftIRXbnjIHQavQwiQSa4skdo(TextStatusActivity.java)
       at com.warrina.sudan.activities.-$$Lambda$TextStatusActivity$XnQftIRXbnjIHQavQwiQSa4skdo.onClick(lambda)
       at android.view.View.performClick(View.java:5721)
       at android.view.View$PerformClick.run(View.java:22620)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:7406)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

This happens randomly

Crashed in 7.0 & 7.1 devices

java.lang.IllegalArgumentException:
at android.view.WindowManagerGlobal.findViewLocked (WindowManagerGlobal.java:508)
at android.view.WindowManagerGlobal.removeView (WindowManagerGlobal.java:417)
at android.view.WindowManagerImpl.removeViewImmediate (WindowManagerImpl.java:128)
at android.app.Dialog.dismissDialog (Dialog.java:446)
at android.app.Dialog.dismiss (Dialog.java:429)
at petrov.kristiyan.colorpicker.ColorPicker$5.run (ColorPicker.java)
at android.os.Handler.handleCallback (Handler.java:751)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6776)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1386)

version1.1.2

Manifest merger failed

Hello im trying to get the latest version of the library into my project "implementation 'petrov.kristiyan:colorpicker-library:1.1.10' but i keep getting this error after syncing greadle

Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
	is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
	Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:17:5-103:19 to override.

use in Fragment?

I use the library in Fragment and i take this error "java.lang.IllegalStateException : You need to use a Theme.AppCompat theme (or descendant) with this activity" ..i read that the problem caused because the activity i am trying to apply the library dialog theme to is extending ActionBarActivity which requires the AppCompat theme to be applied . I tried many solutions but i didnt manage to fix it .
Some help please ?

Thank y and i believe your colorpicker is great!

Layout doesn't utilize space on tablets.

The color picker is now working on tablets, but is not utilizing the space very well. Attached is a screenshot. In this screenshot I am using an array of colors. On vertical layouts all the colors are displayed, in this horizontal layout I have to scroll despite the extra space.

Let me know if you need anymore information or want me to take a stab and implementing a fix.

screenshot_2016-04-01-16-45-33

Default strings aren't localized

It'd be nice if the strings in the library were localized. To do that, we should use string resources instead of hard-coded string constants.

colorpicker without androidx

Hi Kristiyan,

have you a library of this color picker that don't have the androidX support.

I need to import into my project but without last androidx.

Thanks in advance.

Support for tickColor

  • The tick is not visible when i have a white color in the picker
  • Should also support the possibility to change the tick color

suggestion: manipulate properties like text on default buttons or remove access get methods

I am using this library because it is useful to me, but I need to keep using the default buttons, I wanted to change the button text, but it was not possible, only other properties like visibility, I note that there is the method to disable the buttons by default and create my own buttons, that has solved the problem for me, but I would like to be able to edit the text of the default buttons or in your case remove the get methods to access the buttons to guide the user to use the custom methods

   if(positiveButton.getText().toString().trim().isEmpty()){
        positiveButton.setText(positiveText)
    }

    if(negativeButton.getText().toString().trim().isEmpty()){
        negativeButton.setText(positiveText)
    }

or remove this methods

 public Button getPositiveButton() {
        return positiveButton;
    }

 public Button getNegativeButton() {
        return negativeButton;
    }

the examples dont work

WARN - .workspace.NdkWorkspaceManager - NDK support for project 'colorpicker-master' is disabled because it's not a valid Android project
also i tried with only the example and i got
WARN - .workspace.NdkWorkspaceManager - NDK support for project 'example' is disabled because it's not a valid Android project

how can i run the examples ?

colorPicker.setFastChooser wrong in MD

For example 2, change ..
colorPicker.setFastChooser(new ColorPicker.OnFastChooseColorListener() {
.. to ..
colorPicker.setOnFastChooseColorListener(new ColorPicker.OnFastChooseColorListener() {

And if you want a Kotlin example ..

        val colorPicker = ColorPicker(this)
        colorPicker.setColors(hexArrrayList)
        colorPicker.setColumns(4)
        colorPicker.setOnFastChooseColorListener(object : ColorPicker.OnFastChooseColorListener {
      
            override fun setOnFastChooseColorListener(position: Int, color: Int) {
                // put code
            }
            
            override fun onCancel() {
                // put code
            }
        })
        colorPicker.show()

Dialog Background

Is it possible to change the background color of the dialog? How is it possible?

problem with custom set color

Hi my bro.
I have a problem with setColors(ArrayList colorsHexList) method.
when I set my own color by a ArrayList , the dialog nothink show.
What should I do?

this is my code:

ArrayList colorHex = new ArrayList<>();
colorHex.add("0xFF000000");

    final ColorPicker colorPicker = new ColorPicker(this);
    colorPicker.setOnFastChooseColorListener(new ColorPicker.OnFastChooseColorListener() {
        @Override
        public void setOnFastChooseColorListener(int position, int color) {
            showToast(String.valueOf(color));
        }

        @Override
        public void onCancel(){
            // put code
        }
    }).setColors(colorHex).show();

AAPT: error: duplicate value for resource 'attr/strokeWidth' with config ''

After migrating to androidx I am getting this error.


photo5771879951094625036

Error Message
Android resource compilation failed
/home/use/.gradle/caches/transforms-2/files-2.1/81abce46e0344bdf6864fb5ba58dfa35/jetified-color-picker-0.0.5/res/values/values.xml:53:5-368: AAPT: error: duplicate value for resource 'attr/strokeWidth' with config ''.

/home/user/.gradle/caches/transforms-2/files-2.1/81abce46e0344bdf6864fb5ba58dfa35/jetified-color-picker-0.0.5/res/values/values.xml:53:5-368: AAPT: error: resource previously defined here.

/home/user/AndroidStudioProjects/TimeTable/app/build/intermediates/incremental/mergeDebugResources/merged.dir/values/values.xml: AAPT: error: file failed to compile.

After migrating to androidx I am getting this error.
I am using latest gradle version. Kindly resolve the issue, or guide accordingly.

java.lang.NoSuchMethodException - colorpicker-library:1.1.10

E/TypefaceCompatApi21Impl: java.lang.NoSuchMethodException
java.lang.NoSuchMethodException: addFontWeightStyle [class java.lang.String, int, boolean]
at java.lang.Class.getMethod(Class.java:2068)
at java.lang.Class.getMethod(Class.java:1690)
at androidx.core.graphics.TypefaceCompatApi21Impl.(TypefaceCompatApi21Impl.java:74)
at androidx.core.graphics.TypefaceCompat.(TypefaceCompat.java:49)
at androidx.core.graphics.TypefaceCompat.create(TypefaceCompat.java:190)
at androidx.appcompat.widget.AppCompatTextView.setTypeface(AppCompatTextView.java:576)
at androidx.appcompat.widget.AppCompatTextHelper.loadFromAttributes(AppCompatTextHelper.java:217)
at androidx.appcompat.widget.AppCompatTextView.(AppCompatTextView.java:103)
at androidx.appcompat.widget.AppCompatTextView.(AppCompatTextView.java:93)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at petrov.kristiyan.colorpicker.ColorPicker.(ColorPicker.java:80)

Suggestion: Easy "full-range"

For my usage, the examples was not great because a lot of shading were not available in the examples, so I did my own color table. I'd just share a block of code I used to have a simple & efficient color panel. (It's in Kotlin but pretty easy to move to Java, will be happy to do it if you want to.)

Result:

image

Code:

private fun generateColors(width: Int, height: Int): IntArray {
    val colorCount = width * height
    val result = IntArray(colorCount)
    for (index in 0..colorCount - 1) {
        if (index % width == 0) {
            // White->Black
            val hsv = floatArrayOf(0f, 0f, (index / width) / (height - 1).toFloat())
            result[index] = Color.HSVToColor(hsv)
        } else {
            // All colors
            val hsv = floatArrayOf(
                    ((index % width) / (width - 1).toFloat()) * 360f,
                    Math.min(1f, 2f - ((index / width) + 1) / ((height + 1) / 2f)),
                    Math.min(1f, ((index / width) + 1) / ((height + 1) / 2f)))
            result[index] = Color.HSVToColor(hsv)
        }
    }
    return result
}

How to use it:

    colorPicker.setColors(*generateColors(10, 7))
    colorPicker.setColumns(10)

So maybe it could be interesting for the community?

error new version 1.1.7

with 1.1.5 works well but it fail with 1.1.7

java.lang.NullPointerException at
petrov.kristiyan.colorpicker.ColorPicker.dismissDialog(ColorPicker.java:502)
at petrov.kristiyan.colorpicker.ColorPicker.setOnFastChooseColorListener(ColorPicker.java:355)
at myPackage.ChatActivity.onOptionsItemSelected(ChatActivity.java:1257)

final ColorPicker colorPicker = new ColorPicker(this);
            final int init_color;
            if (Build.VERSION.SDK_INT < 23)
                init_color=getResources().getColor(R.color.color1);
            else
                init_color=getResources().getColor(R.color.color1,getTheme());
            colorPicker.setTitle(getString(R.string.chooseColor));
            colorPicker.setRoundColorButton(true);
            colorPicker.setColors(R.array.color_chat_values);
          //////here is the bug//////// 
            colorPicker.setOnFastChooseColorListener(new ColorPicker.OnFastChooseColorListener() {
                @Override
                public void setOnFastChooseColorListener(int position, int color) {
                    SharedPreferences sharedPreferences =
                            PreferenceManager.getDefaultSharedPreferences(ChatActivity.this);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    //posicion empieza desde cero
                    editor.putInt(QuickstartPreferences.THEME_CHAT,position);
                    editor.putInt(QuickstartPreferences.COLOR_CHAT,color);
                    editor.commit();
                    recreate();
                }
                @Override
                public void onCancel(){
                    // put code
                }
            }).setDefaultColorButton(
                    PreferenceManager.getDefaultSharedPreferences(this)
                            .getInt(QuickstartPreferences.COLOR_CHAT,init_color)).setColumns(4).show();

Removal of OnFastChooseListener

Don't see the point in it, i created a fork located https://github.com/KlutzyBubbles/colorpicker/tree/master/colorpicker-library/src/main/java/petrov/kristiyan/colorpicker which contains the changes to remove the interface.

Reasoning: i was developing with the library and found that

  1. the method within the interface was incorrectly named (setOnFastChangeListener instead of onFastChange)
  2. It was extremely frustrating when creating similar code to have to rename the method and the interface for the exact same code (for setting color variables)

An option IF you really dont want to remove the OnFastChangeListener is to make it the same method names as both interfaces were made to provide the same data for the same output just via a different path.

Wanted to create an issue for this before i make a pull request.

ColorPicker DP/PX issue

The method setColorButtonSize requests integer DP that you then convert to PX?!?!:

setColorButtonSize(dip2px(colorButtonWidth, context)

We are passing 48 to the method because this is the min. touchable target size recommended by the Accessibility Scanner. The Scanner then says the views have only 47, so something is wrong in your functions. Anyways why will you set PX if its always recommended to set the sizes of views in DP?
Also dip2px request "float" where you are using it with "int".

How can I now make sure I always get 48dp width and height?

Gradel build fails

Gradle fails to build with compile 'petrov.kristiyan:colorpicker-library:1.1.5' as indicated on the README

Only compile 'petrov.kristiyan.colorpicker:colorpicker-library:1.1.2' works

Dialog resizing

Why is the dialog not automatically resizing when I change size of color buttons? It would be appreciated especially for bigger color buttons.. they just cut a part from themselves to fit in that small dialog

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.