forked from mapzen/prefs-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ru26kif
committed
Aug 26, 2020
1 parent
86c4560
commit 730ce07
Showing
19 changed files
with
687 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.kizitonwose.colorpreference"> | ||
<manifest package="com.kizitonwose.colorpreference"> | ||
|
||
</manifest> | ||
<application /> | ||
|
||
</manifest> |
219 changes: 219 additions & 0 deletions
219
colorpreference/src/main/java/com/kizitonwose/colorpreference/ColorDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
package com.kizitonwose.colorpreference; | ||
|
||
import android.app.Activity; | ||
import android.app.AlertDialog; | ||
import android.app.Dialog; | ||
import android.app.DialogFragment; | ||
import android.content.Context; | ||
import android.content.res.Resources; | ||
import android.os.Bundle; | ||
import android.util.DisplayMetrics; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.widget.GridLayout; | ||
import android.widget.ImageView; | ||
|
||
import androidx.annotation.ArrayRes; | ||
import androidx.annotation.ColorInt; | ||
import androidx.annotation.NonNull; | ||
|
||
/** | ||
* Created by Kizito Nwose on 9/28/2016. | ||
*/ | ||
public class ColorDialog extends DialogFragment { | ||
private GridLayout colorGrid; | ||
private OnColorSelectedListener colorSelectedListener; | ||
private int numColumns; | ||
private int[] colorChoices; | ||
private ColorShape colorShape; | ||
|
||
//the color to be checked | ||
private int selectedColorValue; | ||
|
||
private static final String NUM_COLUMNS_KEY = "num_columns"; | ||
private static final String COLOR_SHAPE_KEY = "color_shape"; | ||
private static final String COLOR_CHOICES_KEY = "color_choices"; | ||
private static final String SELECTED_COLOR_KEY = "selected_color"; | ||
|
||
public ColorDialog() { | ||
} | ||
|
||
public static ColorDialog newInstance(int numColumns, ColorShape colorShape, int[] colorChoices, int selectedColorValue) { | ||
Bundle args = new Bundle(); | ||
args.putInt(NUM_COLUMNS_KEY, numColumns); | ||
args.putSerializable(COLOR_SHAPE_KEY, colorShape); | ||
args.putIntArray(COLOR_CHOICES_KEY, colorChoices); | ||
args.putInt(SELECTED_COLOR_KEY, selectedColorValue); | ||
|
||
ColorDialog dialog = new ColorDialog(); | ||
dialog.setArguments(args); | ||
return dialog; | ||
} | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
Bundle args = getArguments(); | ||
numColumns = args.getInt(NUM_COLUMNS_KEY); | ||
colorShape = (ColorShape) args.getSerializable(COLOR_SHAPE_KEY); | ||
colorChoices = args.getIntArray(COLOR_CHOICES_KEY); | ||
selectedColorValue = args.getInt(SELECTED_COLOR_KEY); | ||
} | ||
|
||
public void setOnColorSelectedListener(OnColorSelectedListener colorSelectedListener) { | ||
this.colorSelectedListener = colorSelectedListener; | ||
repopulateItems(); | ||
} | ||
|
||
@Override | ||
public void onAttach(Context context) { | ||
super.onAttach(context); | ||
|
||
if (context instanceof OnColorSelectedListener) { | ||
setOnColorSelectedListener((OnColorSelectedListener) context); | ||
} else { | ||
repopulateItems(); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public Dialog onCreateDialog(Bundle savedInstanceState) { | ||
LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); | ||
View rootView = layoutInflater.inflate(R.layout.dialog_colors, null); | ||
|
||
colorGrid = rootView.findViewById(R.id.color_grid); | ||
colorGrid.setColumnCount(numColumns); | ||
repopulateItems(); | ||
|
||
return new AlertDialog.Builder(getActivity()) | ||
.setView(rootView) | ||
.create(); | ||
} | ||
|
||
private void repopulateItems() { | ||
if (colorSelectedListener == null || colorGrid == null) { | ||
return; | ||
} | ||
|
||
Context context = colorGrid.getContext(); | ||
colorGrid.removeAllViews(); | ||
for (final int color : colorChoices) { | ||
View itemView = LayoutInflater.from(context) | ||
.inflate(R.layout.grid_item_color, colorGrid, false); | ||
|
||
ColorUtils.setColorViewValue((ImageView) itemView.findViewById(R.id.color_view), color, | ||
color == selectedColorValue, colorShape); | ||
|
||
itemView.setClickable(true); | ||
itemView.setFocusable(true); | ||
itemView.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
if (colorSelectedListener != null) { | ||
colorSelectedListener.onColorSelected(color, getTag()); | ||
} | ||
dismiss(); | ||
} | ||
}); | ||
|
||
colorGrid.addView(itemView); | ||
} | ||
|
||
sizeDialog(); | ||
} | ||
|
||
@Override | ||
public void onStart() { | ||
super.onStart(); | ||
sizeDialog(); | ||
} | ||
|
||
|
||
private void sizeDialog() { | ||
if (colorSelectedListener == null || colorGrid == null) { | ||
return; | ||
} | ||
|
||
Dialog dialog = getDialog(); | ||
if (dialog == null) { | ||
return; | ||
} | ||
|
||
final Resources res = colorGrid.getContext().getResources(); | ||
DisplayMetrics dm = res.getDisplayMetrics(); | ||
|
||
// Can't use Integer.MAX_VALUE here (weird issue observed otherwise on 4.2) | ||
colorGrid.measure( | ||
View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.AT_MOST), | ||
View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.AT_MOST)); | ||
int width = colorGrid.getMeasuredWidth(); | ||
int height = colorGrid.getMeasuredHeight(); | ||
|
||
int extraPadding = res.getDimensionPixelSize(R.dimen.color_grid_extra_padding); | ||
|
||
width += extraPadding; | ||
height += extraPadding; | ||
|
||
dialog.getWindow().setLayout(width, height); | ||
} | ||
|
||
public interface OnColorSelectedListener { | ||
void onColorSelected(int newColor, String tag); | ||
} | ||
|
||
public static class Builder { | ||
private int numColumns = 5; | ||
private int[] colorChoices; | ||
private ColorShape colorShape = ColorShape.CIRCLE; | ||
private Context context; | ||
private int selectedColor; | ||
private String tag; | ||
|
||
|
||
public <ColorActivityType extends Activity & OnColorSelectedListener> Builder(@NonNull ColorActivityType context) { | ||
this.context = context; | ||
//default colors | ||
setColorChoices(R.array.default_color_choice_values); | ||
} | ||
|
||
public Builder setNumColumns(int numColumns) { | ||
this.numColumns = numColumns; | ||
return this; | ||
} | ||
|
||
public Builder setColorChoices(@ArrayRes int colorChoicesRes) { | ||
this.colorChoices = ColorUtils.extractColorArray(colorChoicesRes, context); | ||
return this; | ||
} | ||
|
||
public Builder setColorShape(ColorShape colorShape) { | ||
this.colorShape = colorShape; | ||
return this; | ||
} | ||
|
||
public Builder setSelectedColor(@ColorInt int selectedColor) { | ||
this.selectedColor = selectedColor; | ||
return this; | ||
} | ||
|
||
public Builder setTag(String tag) { | ||
this.tag = tag; | ||
return this; | ||
} | ||
|
||
protected ColorDialog build() { | ||
ColorDialog dialog = ColorDialog.newInstance(numColumns, colorShape, colorChoices, selectedColor); | ||
dialog.setOnColorSelectedListener((OnColorSelectedListener) context); | ||
return dialog; | ||
} | ||
|
||
public ColorDialog show() { | ||
ColorDialog dialog = build(); | ||
dialog.show(Utils.resolveContext(context).getFragmentManager(), tag == null ? String.valueOf(System.currentTimeMillis()) : tag); | ||
return dialog; | ||
} | ||
|
||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
colorpreference/src/main/java/com/kizitonwose/colorpreference/ColorPreference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.kizitonwose.colorpreference; | ||
|
||
import android.content.Context; | ||
import android.content.res.TypedArray; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
import android.widget.ImageView; | ||
|
||
import androidx.preference.Preference; | ||
import androidx.preference.PreferenceViewHolder; | ||
|
||
|
||
public class ColorPreference extends Preference implements ColorDialog.OnColorSelectedListener { | ||
private int[] colorChoices = {}; | ||
private int value = 0; | ||
private int itemLayoutId = R.layout.pref_color_layout; | ||
private int itemLayoutLargeId = R.layout.pref_color_layout_large; | ||
private int numColumns = 5; | ||
private ColorShape colorShape = ColorShape.CIRCLE; | ||
private boolean showDialog = true; | ||
|
||
public ColorPreference(Context context) { | ||
super(context); | ||
initAttrs(null, 0); | ||
} | ||
|
||
public ColorPreference(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
initAttrs(attrs, 0); | ||
} | ||
|
||
public ColorPreference(Context context, AttributeSet attrs, int defStyle) { | ||
super(context, attrs, defStyle); | ||
initAttrs(attrs, defStyle); | ||
} | ||
|
||
private void initAttrs(AttributeSet attrs, int defStyle) { | ||
TypedArray a = getContext().getTheme().obtainStyledAttributes( | ||
attrs, R.styleable.ColorPreference, defStyle, defStyle); | ||
|
||
PreviewSize previewSize = PreviewSize.NORMAL; | ||
try { | ||
numColumns = a.getInteger(R.styleable.ColorPreference_numColumns, numColumns); | ||
colorShape = ColorShape.getShape(a.getInteger(R.styleable.ColorPreference_colorShape, 1)); | ||
previewSize = PreviewSize.getSize(a.getInteger(R.styleable.ColorPreference_viewSize, 1)); | ||
showDialog = a.getBoolean(R.styleable.ColorPreference_showDialog, true); | ||
int choicesResId = a.getResourceId(R.styleable.ColorPreference_colorChoices, | ||
R.array.default_color_choice_values); | ||
colorChoices = ColorUtils.extractColorArray(choicesResId, getContext()); | ||
|
||
} finally { | ||
a.recycle(); | ||
} | ||
|
||
setWidgetLayoutResource(previewSize == PreviewSize.NORMAL ? itemLayoutId : itemLayoutLargeId); | ||
} | ||
|
||
/*@Override | ||
protected void onBindView(View view) { | ||
super.onBindView(view); | ||
ImageView previewView = view.findViewById(R.id.color_view); | ||
ColorUtils.setColorViewValue(previewView, value, false, colorShape); | ||
}*/ | ||
@Override | ||
public void onBindViewHolder (PreferenceViewHolder holder){ | ||
super.onBindViewHolder(holder); | ||
ImageView previewView = (ImageView)holder.findViewById(R.id.color_view); | ||
ColorUtils.setColorViewValue(previewView, value, false, colorShape); | ||
} | ||
|
||
public void setValue(int value) { | ||
if (callChangeListener(value)) { | ||
this.value = value; | ||
persistInt(value); | ||
notifyChanged(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onClick() { | ||
super.onClick(); | ||
if (showDialog) { | ||
ColorUtils.showDialog(getContext(), this, getFragmentTag(), | ||
numColumns, colorShape, colorChoices, getValue()); | ||
} | ||
} | ||
|
||
/*@Override | ||
protected void onAttachedToActivity() { | ||
super.onAttachedToActivity(); | ||
//helps during activity re-creation | ||
if (showDialog) { | ||
ColorUtils.attach(getContext(), this, getFragmentTag()); | ||
} | ||
}*/ | ||
@Override | ||
public void onAttached(){ | ||
super.onAttached(); | ||
//helps during activity re-creation | ||
if (showDialog) { | ||
ColorUtils.attach(getContext(), this, getFragmentTag()); | ||
} | ||
} | ||
|
||
@Override | ||
protected Object onGetDefaultValue(TypedArray a, int index) { | ||
return a.getInt(index, 0); | ||
} | ||
|
||
@Override | ||
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { | ||
setValue(restoreValue ? getPersistedInt(0) : (Integer) defaultValue); | ||
} | ||
|
||
public String getFragmentTag() { | ||
return "color_" + getKey(); | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public void onColorSelected(int newColor, String tag) { | ||
setValue(newColor); | ||
} | ||
} |
Oops, something went wrong.