forked from captswag/material-color-picker
-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Will you add a DialogPreference? #31
Labels
Comments
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. |
@avs333, @Pes8 hey i modifed this code My code hereimport 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 <com.domain.ColorPickerPreference
android:key="ElementColor"
android:summary="Choose color"
android:defaultValue="@color/colorPrimary"
android:title="Color"
<!--AddAlpha="true|false"-->> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!
The text was updated successfully, but these errors were encountered: