I recommend creating a new project or Moudle for testing yourself first.

preface

To tell the truth, I did not understand how to use this framework, after downloading and viewing the author’s source code to know how to use it, no more nonsense, directly into the topic, first look at the effect.

Step 1: Import dependencies

If you want to know the source code and how to implement the function can go to the source address. Import dependencies are as follows:

Implementation 'com. Making. Kizitonwose. Colorpreference: core: 1.1.0'Copy the code

Step 2: Main code

For the convenience of demonstration, the code is simple, the code is as follows:

First: to inherit ColorDialog OnColorSelectedListener method, as shown:

The code is as follows:

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.kizitonwose.colorpreference.ColorDialog;
import com.kizitonwose.colorpreference.ColorShape;

public class MainActivity extends AppCompatActivity implements ColorDialog.OnColorSelectedListener {
    private Button button;
    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button);
    }

    public void colorShare(View view){
        new ColorDialog.Builder(this)
                .setColorShape(ColorShape.SQUARE) //SQUARE the CIRCLE
                .setColorChoices(R.array.color_choices) // Custom colors
                .setNumColumns(5)       // Set the number of color options per row
                .setTag("TAG")        // Echo the onColorSelected method below
                .show();
    }

    @Override
    public void onColorSelected(int newColor, String tag) {
        if(tag=="TAG"){ button.setBackgroundColor(newColor); }}}Copy the code

The main code is as above, and the color selection type is attached below:

<string-array name="color_choices">
        <item>#f44336</item>
        <item>#e91e63</item>
        <item>#9c27b0</item>
        <item>#673ab7</item>
        <item>#3f51b5</item>
        <item>#2196f3</item>
        <item>#03a9f4</item>
        <item>#00bcd4</item>
        <item># 009688</item>
        <item>#4caf50</item>
        <item>#8bc34a</item>
        <item>#cddc39</item>
        <item>#ffeb3b</item>
        <item>#ffc107</item>
        <item>#ff9800</item>
        <item>#ff5722</item>
        <item># 795548</item>
        <item>#9e9e9e</item>
        <item>#607d8b</item>
    </string-array>
Copy the code

Placement position:Finally, let’s look at the simple effect:If you do not understand what you can take a look at the source code, download, set to 0 integral (CSDN can now set 0 integral does not automatically increase or very humane), the source address is as follows:The source address is as follows