Popup Buttons
tgvoskuilen opened this issue · 3 comments
tgvoskuilen commented
Hey, great library! It would be nice however if the Cancel and Choose buttons in the ColorPickerPopup were actual buttons (consistent look and feel with the rest of the app) instead of TextViews, and were on the bottom of the dialog like a standard Android popup dialog.
tgvoskuilen commented
To get the effect I was looking for I added it to a custom DialogFragment. Feel free to use the code!
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import top.defaults.colorpicker.ColorObserver;
import top.defaults.colorpicker.ColorPickerView;
public class ColorPickerDialog extends DialogFragment {
// Library for picker view at:
// https://github.com/duanhong169/ColorPicker
public interface OnColorSelectedCallback {
void onColorSelected(int color);
}
private OnColorSelectedCallback callback;
public void addOnColorSelectedCallback(OnColorSelectedCallback c) {
callback = c;
}
private static final String COLOR = "COLOR";
private int mColor;
public static ColorPickerDialog newInstance(int initialColor) {
ColorPickerDialog f = new ColorPickerDialog();
Bundle args = new Bundle();
args.putInt(COLOR, initialColor);
f.setArguments(args);
return f;
}
public ColorPickerDialog() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getArguments();
if( b != null ) {
mColor = b.getInt(COLOR);
}
}
@Override
public void onDestroyView() {
Dialog dialog = getDialog();
// handles https://code.google.com/p/android/issues/detail?id=17423
if (dialog != null && getRetainInstance()) {
dialog.setDismissMessage(null);
}
super.onDestroyView();
}
@Override
@NonNull
@SuppressLint("InflateParams")
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Activity a = requireActivity();
AlertDialog.Builder b = new AlertDialog.Builder(a);
LayoutInflater inflater = a.getLayoutInflater();
final View dv = inflater.inflate(R.layout.color_picker_dialog, null);
final View colorSample = dv.findViewById(R.id.current_color_sample);
ColorPickerView cpv = dv.findViewById(R.id.color_picker);
cpv.setInitialColor(mColor);
cpv.subscribe(new ColorObserver() {
@Override
public void onColor(int color, boolean fromUser) {
mColor = color;
colorSample.setBackgroundColor(color);
}
});
b.setView(dv).
setPositiveButton(R.string.choose, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if( callback != null ) {
callback.onColorSelected(mColor);
}
}
}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
return b.create();
}
}
and the layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/current_color_sample"
android:layout_width="0dp"
android:layout_height="40dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<top.defaults.colorpicker.ColorPickerView
android:id="@+id/color_picker"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:enableBrightness="true"
app:enableAlpha="false"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/current_color_sample"/>
</android.support.constraint.ConstraintLayout>
duanhong169 commented
Good job! The built-in popup is just an example I think, most of the time we will custom the UI.
rescalon34 commented
@tgvoskuilen nice job man! 👏