Cherish: ColorPicker: make it more user friendly

* Show RGB value instead of ARGB because most average users dont even know about alpha in hex color values
* Make edit text limited to 7 characters and one line
* Remove enter image button

Signed-off-by: 00day0 <therandomuser11@gmail.com>
Signed-off-by: NurKeinNeid <mralexman3000@gmail.com>
This commit is contained in:
00day0
2019-12-11 00:02:58 +01:00
committed by unknown
parent ba9e20e869
commit e0dc6f4d7a
5 changed files with 50 additions and 64 deletions

View File

@@ -26,12 +26,13 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.view.KeyEvent;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import com.android.settings.R;
public class ColorPickerDialog extends AlertDialog implements ColorPickerView.OnColorChangedListener, View.OnClickListener {
public class ColorPickerDialog extends AlertDialog implements ColorPickerView.OnColorChangedListener, View.OnClickListener, View.OnKeyListener {
private ColorPickerView mColorPicker;
private ColorPickerPanelView mOldColor;
@@ -58,6 +59,15 @@ public class ColorPickerDialog extends AlertDialog implements ColorPickerView.On
}
}
private void setColorFromHex() {
String text = mHex.getText().toString();
try {
int newColor = ColorPickerPreference.convertToColorInt(text);
mColorPicker.setColor(newColor, true);
} catch (Exception ignored) {
}
}
private void setUp(int color) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
@@ -71,7 +81,6 @@ public class ColorPickerDialog extends AlertDialog implements ColorPickerView.On
mNewColor = layout.findViewById(R.id.new_color_panel);
mHex = layout.findViewById(R.id.hex);
ImageButton mSetButton = layout.findViewById(R.id.enter);
((LinearLayout) mOldColor.getParent()).setPadding(Math.round(mColorPicker.getDrawingOffset()),
0, Math.round(mColorPicker.getDrawingOffset()), 0);
@@ -83,17 +92,8 @@ public class ColorPickerDialog extends AlertDialog implements ColorPickerView.On
mColorPicker.setColor(color, true);
if (mHex != null) {
mHex.setText(ColorPickerPreference.convertToARGB(color));
}
if (mSetButton != null) {
mSetButton.setOnClickListener(v -> {
String text = mHex.getText().toString();
try {
int newColor = ColorPickerPreference.convertToColorInt(text);
mColorPicker.setColor(newColor, true);
} catch (Exception ignored) {
}
});
mHex.setText(ColorPickerPreference.convertToRGB(color));
mHex.setOnKeyListener(this);
}
setView(layout);
@@ -105,7 +105,7 @@ public class ColorPickerDialog extends AlertDialog implements ColorPickerView.On
mNewColor.setColor(color);
try {
if (mHex != null) {
mHex.setText(ColorPickerPreference.convertToARGB(color));
mHex.setText(ColorPickerPreference.convertToRGB(color));
}
} catch (Exception ignored) {
}
@@ -128,6 +128,16 @@ public class ColorPickerDialog extends AlertDialog implements ColorPickerView.On
return mColorPicker.getColor();
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
setColorFromHex();
return true;
}
return false;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.new_color_panel) {

View File

@@ -302,6 +302,26 @@ public class ColorPickerPreference extends Preference implements
return "#" + alpha + red + green + blue;
}
public static String convertToRGB(int color) {
String red = Integer.toHexString(Color.red(color));
String green = Integer.toHexString(Color.green(color));
String blue = Integer.toHexString(Color.blue(color));
if (red.length() == 1) {
red = "0" + red;
}
if (green.length() == 1) {
green = "0" + green;
}
if (blue.length() == 1) {
blue = "0" + blue;
}
return "#" + red + green + blue;
}
/**
* For custom purposes. Not used by ColorPickerPreferrence
*