android-material-color-picker-dialog icon indicating copy to clipboard operation
android-material-color-picker-dialog copied to clipboard

Will you add a DialogPreference?

Open avs333 opened this issue 7 years ago • 2 comments

Thank you for the code! It'd be cool if you added support for DialogPreference, because the colour selection would often be from the settings of a program, and it'd be handy there! You have to make the private View colorView public and write something like @Override protected View onCreateDialogView() { colorpicker.show(); View vl = (View) colorpicker.colorView.getParent(); ViewGroup vp = (ViewGroup) vl.getParent(); if(vp != null) vp.removeView(vl); return vl; } I wish you luck!

avs333 avatar Sep 14 '18 00:09 avs333

Hi! I'm open to receive pull request, so if you want to implement it I'll merge the PR and add your name to the collaborators :)

Unfortunately at this moment I haven't time to invest in this project.

Pes8 avatar Sep 14 '18 07:09 Pes8

@avs333, @Pes8 hey i modifed this code

My code here
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.pes.androidmaterialcolorpickerdialog.ColorPicker;
import com.pes.androidmaterialcolorpickerdialog.ColorPickerCallback;

public class ColorPickerPreference 
extends Preference 
implements Preference.OnPreferenceClickListener, ColorPickerCallback {

    View mView;
    ColorPicker mColorPicker;
    private int mValue = Color.BLACK;
    private float mDensity = 0;
    private boolean mAlphaEnabled = false;
    private Bitmap.Config mConfig = Bitmap.Config.RGB_565;

    public ColorPickerPreference(Context context) {
        super(context);
        init(context, null);
    }

    public ColorPickerPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ColorPickerPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        mColorPicker.setColor(mValue);
        mColorPicker.enableAutoClose();
        mColorPicker.setCallback(this);
        mColorPicker.show();
        return false;
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getColor(index, Color.BLACK);
    }

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        onColorChosen(restoreValue ? getPersistedInt(mValue) : (Integer) defaultValue);
    }

    private void init(Context context, AttributeSet attrs) {
        mDensity = getContext().getResources().getDisplayMetrics().density;
        setOnPreferenceClickListener(this);
        if (attrs != null)
            mAlphaEnabled = attrs.getAttributeBooleanValue(null, "AddAlpha", false);
        if (mAlphaEnabled) {
            mConfig = Bitmap.Config.ARGB_8888;
            mColorPicker = new ColorPicker((Activity) context,0,0,0,0);
        } else
            mColorPicker = new ColorPicker((Activity) context);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        mView = view;
        setPreviewColor();
    }

    private void setPreviewColor() {
        if (mView == null) return;
        ImageView iView = new ImageView(getContext());
        LinearLayout widgetFrameView = ((LinearLayout) mView.findViewById(android.R.id.widget_frame));
        if (widgetFrameView == null) return;
        widgetFrameView.setVisibility(View.VISIBLE);
        widgetFrameView.setPadding(
                widgetFrameView.getPaddingLeft(),
                widgetFrameView.getPaddingTop(),
                (int) (mDensity * 8),
                widgetFrameView.getPaddingBottom()
        );
        // remove already create preview image
        widgetFrameView.removeAllViews();
        widgetFrameView.addView(iView);
        widgetFrameView.setMinimumWidth(0);
        iView.setImageBitmap(getPreviewBitmap());
    }

    private Bitmap getPreviewBitmap() {
        int d = (int) (mDensity * 31); //30dip
        Bitmap bm = Bitmap.createBitmap(d, d, mConfig);
        int w = bm.getWidth();
        int h = bm.getHeight();
        int c = mValue;
        for (int i = 0; i < w; i++) {
            for (int j = i; j < h; j++) {
                c = (i <= 1 || j <= 1 || i >= w - 2 || j >= h - 2) ? Color.GRAY : mValue;
                bm.setPixel(i, j, c);
                if (i != j) {
                    bm.setPixel(j, i, c);
                }
            }
        }

        return bm;
    }

    @Override
    public void onColorChosen(int color) {
        if (isPersistent())
            persistInt(color);
        mValue = color;
        setPreviewColor();
        if (getOnPreferenceChangeListener() != null)
            getOnPreferenceChangeListener().onPreferenceChange(this, color);
    }
}

works fine Example:


<com.domain.ColorPickerPreference
                android:key="ElementColor"
                android:summary="Choose color"
                android:defaultValue="@color/colorPrimary"
                android:title="Color"
                <!--AddAlpha="true|false"-->>

long76 avatar Mar 10 '19 17:03 long76