Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
compileSdk : 28,
targetSdk : 28,
minSdk : 14,
gradlePlugin : '3.3.0'
gradlePlugin : '3.4.1'
]
ext.versions = [
androidx : '1.0.2',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,64 +25,71 @@
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jaredrummler.android.colorpicker.ColorPickerDialog;
import com.jaredrummler.android.colorpicker.ColorPickerDialogListener;

public class MainActivity extends AppCompatActivity implements ColorPickerDialogListener {

private static final String TAG = "MainActivity";
private static final String TAG = "MainActivity";

// Give your color picker dialog unique IDs if you have multiple dialogs.
private static final int DIALOG_ID = 0;
// Give your color picker dialog unique IDs if you have multiple dialogs.
private static final int DIALOG_ID = 0;


@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(android.R.id.content, new DemoFragment()).commit();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(android.R.id.content, new DemoFragment()).commit();
}
}
}

@Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_color_picker_dialog:
ColorPickerDialog.newBuilder()
.setDialogType(ColorPickerDialog.TYPE_CUSTOM)
.setAllowPresets(false)
.setDialogId(DIALOG_ID)
.setColor(Color.BLACK)
.setShowAlphaSlider(true)
.show(this);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
case R.id.menu_github:
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/jaredrummler/ColorPicker")));
} catch (ActivityNotFoundException ignored) {
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_color_picker_dialog:
ColorPickerDialog.newBuilder()
.setDialogType(ColorPickerDialog.TYPE_CUSTOM)
.setAllowPresets(false)
.setDialogId(DIALOG_ID)
.setColor(Color.BLACK)
.setShowAlphaSlider(true)
.show(this);
return true;
case R.id.menu_github:
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/jaredrummler/ColorPicker")));
} catch (ActivityNotFoundException ignored) {
}
return true;
}
return true;
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}

@Override public void onColorSelected(int dialogId, int color) {
Log.d(TAG, "onColorSelected() called with: dialogId = [" + dialogId + "], color = [" + color + "]");
switch (dialogId) {
case DIALOG_ID:
// We got result from the dialog that is shown when clicking on the icon in the action bar.
Toast.makeText(MainActivity.this, "Selected Color: #" + Integer.toHexString(color), Toast.LENGTH_SHORT).show();
break;
@Override
public void onColorSelected(int dialogId, int color) {
Log.d(TAG, "onColorSelected() called with: dialogId = [" + dialogId + "], color = [" + color + "]");
switch (dialogId) {
case DIALOG_ID:
// We got result from the dialog that is shown when clicking on the icon in the action bar.
Toast.makeText(MainActivity.this, "Selected Color: #" + Integer.toHexString(color), Toast.LENGTH_SHORT).show();
break;
}
}
}

@Override public void onDialogDismissed(int dialogId) {
Log.d(TAG, "onDialogDismissed() called with: dialogId = [" + dialogId + "]");
}
@Override
public void onDialogDismissed(int dialogId) {
Log.d(TAG, "onDialogDismissed() called with: dialogId = [" + dialogId + "]");
}

}

2 changes: 1 addition & 1 deletion demo/src/main/res/values-v21/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<!-- Styling the color picker dialog fragment. -->
<eat-comment/>
<!-- Below are some styles that show you how to change the apperance of the color picker dialog. -->
<!-- Below are some styles that show you how to change the appearance of the color picker dialog. -->
<eat-comment/>

<style name="LightPickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
Expand Down
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Tue Jun 18 11:19:19 CEST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.core.graphics.ColorUtils;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;

import java.util.Arrays;
import java.util.Locale;

Expand All @@ -70,12 +74,12 @@ public class ColorPickerDialog extends DialogFragment implements ColorPickerView
private static final String TAG = "ColorPickerDialog";

public static final int TYPE_CUSTOM = 0;
public static final int TYPE_PRESETS = 1;
static final int TYPE_PRESETS = 1;

/**
* Material design colors used as the default color presets
*/
public static final int[] MATERIAL_COLORS = {
static final int[] MATERIAL_COLORS = {
0xFFF44336, // RED 500
0xFFE91E63, // PINK 500
0xFFFF2C93, // LIGHT PINK 500
Expand Down Expand Up @@ -112,30 +116,32 @@ public class ColorPickerDialog extends DialogFragment implements ColorPickerView
private static final String ARG_PRESETS_BUTTON_TEXT = "presetsButtonText";
private static final String ARG_CUSTOM_BUTTON_TEXT = "customButtonText";
private static final String ARG_SELECTED_BUTTON_TEXT = "selectedButtonText";
private static final String ARG_DIALOG_STYLE = "dialogStyle";

ColorPickerDialogListener colorPickerDialogListener;
FrameLayout rootView;
int[] presets;
@ColorInt int color;
int dialogType;
int dialogId;
boolean showColorShades;
int colorShape;
private ColorPickerDialogListener colorPickerDialogListener;
private FrameLayout rootView;
private int[] presets;
@ColorInt private int color;
private int dialogType;
private int dialogId;
private boolean showColorShades;
private int colorShape;

// -- PRESETS --------------------------
ColorPaletteAdapter adapter;
LinearLayout shadesLayout;
SeekBar transparencySeekBar;
TextView transparencyPercText;
private ColorPaletteAdapter adapter;
private LinearLayout shadesLayout;
private SeekBar transparencySeekBar;
private TextView transparencyPercText;

// -- CUSTOM ---------------------------
ColorPickerView colorPicker;
ColorPanelView newColorPanel;
EditText hexEditText;
private ColorPickerView colorPicker;
private ColorPanelView newColorPanel;
private EditText hexEditText;
boolean showAlphaSlider;
private int presetsButtonStringRes;
private boolean fromEditText;
private int customButtonStringRes;
private TextView hexHashTextView;

private final OnTouchListener onPickerTouchListener = new OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
Expand Down Expand Up @@ -171,20 +177,26 @@ public static Builder newBuilder() {
color = savedInstanceState.getInt(ARG_COLOR);
dialogType = savedInstanceState.getInt(ARG_TYPE);
}
int styleResource = getArguments().getInt(ARG_DIALOG_STYLE);

Context wrappedContext = new ContextThemeWrapper(requireActivity(), styleResource);

AlertDialog.Builder builder = new AlertDialog.Builder(wrappedContext);

rootView = new FrameLayout(wrappedContext);

rootView = new FrameLayout(requireActivity());
if (dialogType == TYPE_CUSTOM) {
rootView.addView(createPickerView());
rootView.addView(createPickerView(wrappedContext));
} else if (dialogType == TYPE_PRESETS) {
rootView.addView(createPresetsView());
rootView.addView(createPresetsView(wrappedContext));
}

int selectedButtonStringRes = getArguments().getInt(ARG_SELECTED_BUTTON_TEXT);
if (selectedButtonStringRes == 0) {
selectedButtonStringRes = R.string.cpv_select;
}

AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity()).setView(rootView)
builder.setView(rootView)
.setPositiveButton(selectedButtonStringRes, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
onColorSelected(color);
Expand Down Expand Up @@ -217,7 +229,7 @@ public static Builder newBuilder() {

@Override public void onStart() {
super.onStart();
AlertDialog dialog = (AlertDialog) getDialog();
final AlertDialog dialog = (AlertDialog) getDialog();

// http://stackoverflow.com/a/16972670/1048340
//noinspection ConstantConditions
Expand All @@ -235,12 +247,12 @@ public static Builder newBuilder() {
case TYPE_CUSTOM:
dialogType = TYPE_PRESETS;
((Button) v).setText(customButtonStringRes != 0 ? customButtonStringRes : R.string.cpv_custom);
rootView.addView(createPresetsView());
rootView.addView(createPresetsView(dialog.getContext()));
break;
case TYPE_PRESETS:
dialogType = TYPE_CUSTOM;
((Button) v).setText(presetsButtonStringRes != 0 ? presetsButtonStringRes : R.string.cpv_presets);
rootView.addView(createPickerView());
rootView.addView(createPickerView(dialog.getContext()));
}
}
});
Expand Down Expand Up @@ -272,21 +284,24 @@ public void setColorPickerDialogListener(ColorPickerDialogListener colorPickerDi

// region Custom Picker

View createPickerView() {
View contentView = View.inflate(getActivity(), R.layout.cpv_dialog_color_picker, null);
colorPicker = (ColorPickerView) contentView.findViewById(R.id.cpv_color_picker_view);
ColorPanelView oldColorPanel = (ColorPanelView) contentView.findViewById(R.id.cpv_color_panel_old);
newColorPanel = (ColorPanelView) contentView.findViewById(R.id.cpv_color_panel_new);
ImageView arrowRight = (ImageView) contentView.findViewById(R.id.cpv_arrow_right);
hexEditText = (EditText) contentView.findViewById(R.id.cpv_hex);
private View createPickerView(Context context) {
View contentView = View.inflate(context, R.layout.cpv_dialog_color_picker, null);
colorPicker = contentView.findViewById(R.id.cpv_color_picker_view);
ColorPanelView oldColorPanel = contentView.findViewById(R.id.cpv_color_panel_old);
newColorPanel = contentView.findViewById(R.id.cpv_color_panel_new);
ImageView arrowRight = contentView.findViewById(R.id.cpv_arrow_right);
hexEditText = contentView.findViewById(R.id.cpv_hex);
hexHashTextView = contentView.findViewById(R.id.hex_hash_text_view);

try {
final TypedValue value = new TypedValue();
TypedArray typedArray =
getActivity().obtainStyledAttributes(value.data, new int[] { android.R.attr.textColorPrimary });
int arrowColor = typedArray.getColor(0, Color.BLACK);
int hashColor = typedArray.getColor(R.styleable.ColorPickerView_cpv_hashColor, Color.BLACK);
typedArray.recycle();
arrowRight.setColorFilter(arrowColor);
hexHashTextView.setTextColor(hashColor);
} catch (Exception ignored) {
}

Expand Down Expand Up @@ -425,12 +440,12 @@ private int parseColorString(String colorString) throws NumberFormatException {

// region Presets Picker

View createPresetsView() {
View contentView = View.inflate(getActivity(), R.layout.cpv_dialog_presets, null);
shadesLayout = (LinearLayout) contentView.findViewById(R.id.shades_layout);
transparencySeekBar = (SeekBar) contentView.findViewById(R.id.transparency_seekbar);
transparencyPercText = (TextView) contentView.findViewById(R.id.transparency_text);
GridView gridView = (GridView) contentView.findViewById(R.id.gridView);
private View createPresetsView(Context context) {
View contentView = View.inflate(context, R.layout.cpv_dialog_presets, null);
shadesLayout = contentView.findViewById(R.id.shades_layout);
transparencySeekBar = contentView.findViewById(R.id.transparency_seekbar);
transparencyPercText = contentView.findViewById(R.id.transparency_text);
GridView gridView = contentView.findViewById(R.id.gridView);

loadPresets();

Expand Down Expand Up @@ -496,7 +511,7 @@ private void loadPresets() {
}
}

void createColorShades(@ColorInt final int color) {
private void createColorShades(@ColorInt final int color) {
final int[] colorShades = getColorShades(color);

if (shadesLayout.getChildCount() != 0) {
Expand Down Expand Up @@ -733,7 +748,8 @@ private int getSelectedItemPosition() {

// region Builder

@IntDef({ TYPE_CUSTOM, TYPE_PRESETS }) public @interface DialogType {
@IntDef({ TYPE_CUSTOM, TYPE_PRESETS })
@interface DialogType {

}

Expand All @@ -753,6 +769,7 @@ public static final class Builder {
boolean allowCustom = true;
boolean showColorShades = true;
@ColorShape int colorShape = ColorShape.CIRCLE;
@StyleRes int styleResource;

/*package*/ Builder() {

Expand Down Expand Up @@ -902,6 +919,11 @@ public Builder setColorShape(int colorShape) {
return this;
}

public Builder setDialogStyle(@StyleRes int styleResource) {
this.styleResource = styleResource;
return this;
}

/**
* Create the {@link ColorPickerDialog} instance.
*
Expand All @@ -924,6 +946,7 @@ public ColorPickerDialog create() {
args.putInt(ARG_PRESETS_BUTTON_TEXT, presetsButtonText);
args.putInt(ARG_CUSTOM_BUTTON_TEXT, customButtonText);
args.putInt(ARG_SELECTED_BUTTON_TEXT, selectedButtonText);
args.putInt(ARG_DIALOG_STYLE, styleResource);
dialog.setArguments(args);
return dialog;
}
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/layout/cpv_dialog_color_picker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
>

<TextView
android:id="@+id/hex_hash_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#"
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<attr format="string|reference" name="cpv_alphaChannelText"/>
<attr format="color|reference" name="cpv_sliderColor"/>
<attr name="cpv_borderColor"/>
<attr name="cpv_hashColor" format="color|reference"/>
</declare-styleable>

<declare-styleable name="ColorPreference" parent="ColorPickerDialog">
Expand Down