Skip to content

Commit 15b666c

Browse files
committed
Merge pull request #23 from timothyjc/hex-tweaks
Hex picker tweaks
2 parents 0a4c1f2 + b378b8d commit 15b666c

File tree

5 files changed

+43
-50
lines changed

5 files changed

+43
-50
lines changed

res/layout-land/dialog_color_picker.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
android:layout_width="fill_parent"
4040
android:layout_height="wrap_content"
4141
android:hint="HEX"
42+
android:imeOptions="actionDone"
4243
android:maxLength="7"
44+
android:singleLine="true"
45+
android:inputType="textCapCharacters"
4346
android:visibility="gone" >
4447
</EditText>
4548

res/layout/dialog_color_picker.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@
4949
android:layout_height="wrap_content"
5050
android:layout_weight="1"
5151
android:hint="HEX"
52+
android:imeOptions="actionDone"
5253
android:maxLength="7"
54+
android:singleLine="true"
55+
android:inputType="textCapCharacters"
5356
android:visibility="gone" >
5457
</EditText>
5558
</LinearLayout>

src/net/margaritov/preference/colorpicker/ColorPickerDialog.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@
1616

1717
package net.margaritov.preference.colorpicker;
1818

19+
import java.util.Locale;
20+
1921
import android.app.Dialog;
2022
import android.content.Context;
2123
import android.content.res.ColorStateList;
2224
import android.graphics.Color;
2325
import android.graphics.PixelFormat;
2426
import android.os.Bundle;
25-
import android.text.Editable;
2627
import android.text.InputFilter;
27-
import android.text.TextWatcher;
28+
import android.text.InputType;
29+
import android.view.KeyEvent;
2830
import android.view.LayoutInflater;
2931
import android.view.View;
32+
import android.view.inputmethod.EditorInfo;
33+
import android.view.inputmethod.InputMethodManager;
3034
import android.widget.EditText;
3135
import android.widget.LinearLayout;
36+
import android.widget.TextView;
3237

3338
public class ColorPickerDialog
3439
extends
@@ -43,7 +48,6 @@ public class ColorPickerDialog
4348
private ColorPickerPanelView mNewColor;
4449

4550
private EditText mHexVal;
46-
private boolean mHexInternalTextChange;
4751
private boolean mHexValueEnabled = false;
4852
private ColorStateList mHexDefaultTextColor;
4953

@@ -82,28 +86,31 @@ private void setUp(int color) {
8286
mNewColor = (ColorPickerPanelView) layout.findViewById(R.id.new_color_panel);
8387

8488
mHexVal = (EditText) layout.findViewById(R.id.hex_val);
89+
mHexVal.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
8590
mHexDefaultTextColor = mHexVal.getTextColors();
86-
mHexVal.addTextChangedListener(new TextWatcher() {
87-
@Override
88-
public void onTextChanged(CharSequence s, int start, int before, int count) {}
89-
@Override
90-
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
91+
92+
mHexVal.setOnEditorActionListener(new TextView.OnEditorActionListener() {
93+
9194
@Override
92-
public void afterTextChanged(Editable s) {
93-
if (mHexValueEnabled) {
94-
if (mHexInternalTextChange) return;
95-
95+
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
96+
if (actionId == EditorInfo.IME_ACTION_DONE) {
97+
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
98+
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
99+
String s = mHexVal.getText().toString();
96100
if (s.length() > 5 || s.length() < 10) {
97101
try {
98102
int c = ColorPickerPreference.convertToColorInt(s.toString());
99103
mColorPicker.setColor(c, true);
100104
mHexVal.setTextColor(mHexDefaultTextColor);
101-
} catch (NumberFormatException e) {
105+
} catch (IllegalArgumentException e) {
102106
mHexVal.setTextColor(Color.RED);
103107
}
104-
} else
108+
} else {
105109
mHexVal.setTextColor(Color.RED);
110+
}
111+
return true;
106112
}
113+
return false;
107114
}
108115
});
109116

@@ -161,12 +168,12 @@ private void updateHexLengthFilter() {
161168
}
162169

163170
private void updateHexValue(int color) {
164-
mHexInternalTextChange = true;
165-
if (getAlphaSliderVisible())
166-
mHexVal.setText(ColorPickerPreference.convertToARGB(color));
167-
else
168-
mHexVal.setText(ColorPickerPreference.convertToRGB(color));
169-
mHexInternalTextChange = false;
171+
if (getAlphaSliderVisible()) {
172+
mHexVal.setText(ColorPickerPreference.convertToARGB(color).toUpperCase(Locale.getDefault()));
173+
} else {
174+
mHexVal.setText(ColorPickerPreference.convertToRGB(color).toUpperCase(Locale.getDefault()));
175+
}
176+
mHexVal.setTextColor(mHexDefaultTextColor);
170177
}
171178

172179
public void setAlphaSliderVisible(boolean visible) {

src/net/margaritov/preference/colorpicker/ColorPickerPreference.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -245,31 +245,14 @@ public static String convertToRGB(int color) {
245245
* @throws NumberFormatException
246246
* @author Unknown
247247
*/
248-
public static int convertToColorInt(String argb) throws NumberFormatException {
248+
public static int convertToColorInt(String argb) throws IllegalArgumentException {
249249

250-
if (argb.startsWith("#")) {
251-
argb = argb.replace("#", "");
252-
}
253-
254-
int alpha = -1, red = -1, green = -1, blue = -1;
255-
256-
if (argb.length() == 8) {
257-
alpha = Integer.parseInt(argb.substring(0, 2), 16);
258-
red = Integer.parseInt(argb.substring(2, 4), 16);
259-
green = Integer.parseInt(argb.substring(4, 6), 16);
260-
blue = Integer.parseInt(argb.substring(6, 8), 16);
261-
}
262-
else if (argb.length() == 6) {
263-
alpha = 255;
264-
red = Integer.parseInt(argb.substring(0, 2), 16);
265-
green = Integer.parseInt(argb.substring(2, 4), 16);
266-
blue = Integer.parseInt(argb.substring(4, 6), 16);
267-
}
268-
else
269-
throw new NumberFormatException("string " + argb + "did not meet length requirements");
250+
if (!argb.startsWith("#")) {
251+
argb = "#" + argb;
252+
}
270253

271-
return Color.argb(alpha, red, green, blue);
272-
}
254+
return Color.parseColor(argb);
255+
}
273256

274257
@Override
275258
protected Parcelable onSaveInstanceState() {

src/net/margaritov/preference/colorpicker/ColorPickerView.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -848,24 +848,21 @@ public void setColor(int color){
848848
* @param callback If you want to get a callback to
849849
* your OnColorChangedListener.
850850
*/
851-
public void setColor(int color, boolean callback){
851+
public void setColor(int color, boolean callback) {
852852

853853
int alpha = Color.alpha(color);
854-
int red = Color.red(color);
855-
int blue = Color.blue(color);
856-
int green = Color.green(color);
857854

858855
float[] hsv = new float[3];
859856

860-
Color.RGBToHSV(red, green, blue, hsv);
857+
Color.colorToHSV(color, hsv);
861858

862859
mAlpha = alpha;
863860
mHue = hsv[0];
864861
mSat = hsv[1];
865862
mVal = hsv[2];
866863

867-
if(callback && mListener != null){
868-
mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}));
864+
if (callback && mListener != null) {
865+
mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[] { mHue, mSat, mVal }));
869866
}
870867

871868
invalidate();

0 commit comments

Comments
 (0)