Skip to content

Commit 806e641

Browse files
committed
fix #18
1 parent eac3731 commit 806e641

File tree

6 files changed

+108
-134
lines changed

6 files changed

+108
-134
lines changed

.idea/dictionaries/carbonyl.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/src/main/res/layout/activity_main.xml

+14-11
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@
6060
app:maxCharacters="20">
6161

6262
<studio.carbonylgroup.textfieldboxes.ExtendedEditText
63-
android:layout_width="wrap_content"
63+
android:layout_width="match_parent"
6464
android:layout_height="wrap_content"
65-
android:singleLine="true"
66-
android:text="Vintage 50's Dress" />
65+
android:inputType="text"
66+
android:maxLines="1"
67+
android:text="@string/title" />
6768

6869
</studio.carbonylgroup.textfieldboxes.TextFieldBoxes>
6970

@@ -85,10 +86,11 @@
8586
app:labelText="Price">
8687

8788
<studio.carbonylgroup.textfieldboxes.ExtendedEditText
88-
android:layout_width="wrap_content"
89+
android:layout_width="match_parent"
8990
android:layout_height="wrap_content"
90-
android:singleLine="true"
91-
android:text="10.00"
91+
android:inputType="text"
92+
android:maxLines="1"
93+
android:text="@string/price"
9294
app:prefix="$ " />
9395

9496
</studio.carbonylgroup.textfieldboxes.TextFieldBoxes>
@@ -106,9 +108,10 @@
106108
app:labelText="Location (optional)">
107109

108110
<studio.carbonylgroup.textfieldboxes.ExtendedEditText
109-
android:layout_width="wrap_content"
111+
android:layout_width="match_parent"
110112
android:layout_height="wrap_content"
111-
android:singleLine="true"
113+
android:inputType="text"
114+
android:maxLines="1"
112115
android:text="" />
113116

114117
</studio.carbonylgroup.textfieldboxes.TextFieldBoxes>
@@ -127,9 +130,9 @@
127130

128131
<studio.carbonylgroup.textfieldboxes.ExtendedEditText
129132
android:id="@+id/extendedEditText"
130-
android:layout_width="wrap_content"
133+
android:layout_width="match_parent"
131134
android:layout_height="wrap_content"
132-
android:text="Unique and rare dress from 1952. Made out of washed cotton with front pockets. Sleeveless with button closures." />
135+
android:text="@string/description" />
133136

134137
</studio.carbonylgroup.textfieldboxes.TextFieldBoxes>
135138

@@ -138,7 +141,7 @@
138141
android:layout_width="match_parent"
139142
android:layout_height="wrap_content"
140143
android:layout_marginTop="30dp"
141-
android:text="DARK SIDE" />
144+
android:text="@string/dark_side" />
142145

143146
</LinearLayout>
144147

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<resources>
22
<string name="app_name">TextFieldBoxes</string>
3+
<string name="title">Vintage 50\'s Dress</string>
4+
<string name="price">10.00</string>
5+
<string name="description">Unique and rare dress from 1952. Made out of washed cotton with front pockets. Sleeveless with button closures.</string>
6+
<string name="dark_side">DARK SIDE</string>
37
</resources>

textfieldboxes/src/main/java/studio/carbonylgroup/textfieldboxes/ExtendedEditText.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.graphics.Paint;
99
import android.graphics.PixelFormat;
1010
import android.graphics.drawable.Drawable;
11+
import android.support.annotation.NonNull;
1112
import android.support.design.widget.TextInputEditText;
1213
import android.util.AttributeSet;
1314

@@ -16,6 +17,7 @@
1617
* Text Field Boxes
1718
* Created by CarbonylGroup on 2017/09/01
1819
*/
20+
@SuppressWarnings("unused")
1921
public class ExtendedEditText extends TextInputEditText {
2022

2123
public int DEFAULT_TEXT_COLOR;
@@ -119,7 +121,7 @@ private TextDrawable() {
119121
}
120122

121123
@Override
122-
public void draw(Canvas canvas) {
124+
public void draw(@NonNull Canvas canvas) {
123125

124126
final int lineBase = getLineBounds(0, null);
125127
final int lineBottom = getLineBounds(getLineCount() - 1, null);

textfieldboxes/src/main/java/studio/carbonylgroup/textfieldboxes/TextFieldBoxes.java

+78-11
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import android.content.res.Resources;
55
import android.content.res.TypedArray;
66
import android.graphics.Color;
7-
import android.graphics.drawable.GradientDrawable;
8-
import android.graphics.drawable.LayerDrawable;
7+
import android.graphics.PorterDuff;
8+
import android.graphics.PorterDuffColorFilter;
9+
import android.graphics.drawable.Drawable;
10+
import android.support.v4.content.ContextCompat;
911
import android.support.v4.view.ViewCompat;
1012
import android.support.v7.widget.AppCompatImageButton;
1113
import android.support.v7.widget.AppCompatTextView;
@@ -16,14 +18,19 @@
1618
import android.view.View;
1719
import android.view.ViewGroup;
1820
import android.view.inputmethod.InputMethodManager;
21+
import android.widget.EditText;
1922
import android.widget.FrameLayout;
2023
import android.widget.RelativeLayout;
24+
import android.widget.TextView;
25+
26+
import java.lang.reflect.Field;
2127

2228

2329
/**
2430
* Text Field Boxes
2531
* Created by CarbonylGroup on 2017/08/25
2632
*/
33+
@SuppressWarnings("unused")
2734
public class TextFieldBoxes extends FrameLayout {
2835

2936
/**
@@ -155,16 +162,16 @@ protected void initDefaultColor() {
155162
TypedArray themeArray;
156163

157164
/* Get Default Error Color From Theme */
158-
DEFAULT_ERROR_COLOR = getContext().getResources().getColor(R.color.A400red);
165+
DEFAULT_ERROR_COLOR = ContextCompat.getColor(getContext(), R.color.A400red);
159166

160167
/* Get Default Background Color From Theme */
161168
themeArray = theme.obtainStyledAttributes(new int[]{android.R.attr.colorForeground});
162-
DEFAULT_BG_COLOR = Utils.adjustAlpha(themeArray.getColor(0, 0), 0.06f);
169+
DEFAULT_BG_COLOR = adjustAlpha(themeArray.getColor(0, 0), 0.06f);
163170

164171
/* Get Default Primary Color From Theme */
165172
themeArray = theme.obtainStyledAttributes(new int[]{R.attr.colorPrimary});
166-
if (Utils.isLight(DEFAULT_BG_COLOR))
167-
DEFAULT_PRIMARY_COLOR = Utils.lighter(themeArray.getColor(0, 0), 0.2f);
173+
if (isLight(DEFAULT_BG_COLOR))
174+
DEFAULT_PRIMARY_COLOR = lighter(themeArray.getColor(0, 0), 0.2f);
168175
else DEFAULT_PRIMARY_COLOR = themeArray.getColor(0, 0);
169176

170177
/* Get Default Text Color From Theme */
@@ -175,7 +182,7 @@ protected void initDefaultColor() {
175182
themeArray = theme.obtainStyledAttributes(new int[]{android.R.attr.disabledAlpha});
176183
float disabledAlpha = themeArray.getFloat(0, 0);
177184
themeArray = theme.obtainStyledAttributes(new int[]{android.R.attr.textColorTertiary});
178-
DEFAULT_DISABLED_TEXT_COLOR = Utils.adjustAlpha(themeArray.getColor(0, 0), disabledAlpha);
185+
DEFAULT_DISABLED_TEXT_COLOR = adjustAlpha(themeArray.getColor(0, 0), disabledAlpha);
179186

180187
themeArray.recycle();
181188
}
@@ -526,7 +533,7 @@ protected void makeCursorBlink() {
526533
protected void setHighlightColor(int colorRes) {
527534

528535
this.floatingLabel.setTextColor(colorRes);
529-
Utils.setCursorDrawableColor(this.editText, colorRes);
536+
setCursorDrawableColor(this.editText, colorRes);
530537

531538
if (getIsResponsiveIconColor()) {
532539
this.iconImageButton.setColorFilter(colorRes);
@@ -555,7 +562,8 @@ protected void updateCounterText() {
555562
else showClearButton(true);
556563

557564
/* Don't Count Space & Line Feed */
558-
int length = this.editText.getText().toString().replaceAll(" ", "").replaceAll("\n", "").length();
565+
int length = this.editText.getText().toString()
566+
.replaceAll(" ", "").replaceAll("\n", "").length();
559567
String lengthStr = Integer.toString(length) + " / ";
560568

561569
if (this.maxCharacters > 0) {
@@ -694,8 +702,8 @@ public void setPrimaryColor(int colorRes) {
694702
public void setPanelBackgroundColor(int colorRes) {
695703

696704
this.panelBackgroundColor = colorRes;
697-
((GradientDrawable) ((LayerDrawable) this.panel.getBackground())
698-
.findDrawableByLayerId(R.id.bg_cover)).setColor(panelBackgroundColor);
705+
this.panel.getBackground()
706+
.setColorFilter(new PorterDuffColorFilter(colorRes, PorterDuff.Mode.SRC_IN));
699707
}
700708

701709
/* Characters Counter Setters */
@@ -939,4 +947,63 @@ public boolean getHasFocus() {
939947
public boolean getIsResponsiveIconColor() {
940948
return this.isResponsiveIconColor;
941949
}
950+
951+
/**
952+
* set EditText cursor color
953+
*/
954+
protected static void setCursorDrawableColor(EditText _editText, int _colorRes) {
955+
956+
try {
957+
Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
958+
fCursorDrawableRes.setAccessible(true);
959+
int mCursorDrawableRes = fCursorDrawableRes.getInt(_editText);
960+
Field fEditor = TextView.class.getDeclaredField("mEditor");
961+
fEditor.setAccessible(true);
962+
Object editor = fEditor.get(_editText);
963+
Class<?> clazz = editor.getClass();
964+
Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
965+
fCursorDrawable.setAccessible(true);
966+
Drawable[] drawables = new Drawable[2];
967+
drawables[0] = ContextCompat.getDrawable(_editText.getContext(), mCursorDrawableRes);
968+
drawables[1] = ContextCompat.getDrawable(_editText.getContext(), mCursorDrawableRes);
969+
drawables[0].setColorFilter(_colorRes, PorterDuff.Mode.SRC_IN);
970+
drawables[1].setColorFilter(_colorRes, PorterDuff.Mode.SRC_IN);
971+
fCursorDrawable.set(editor, drawables);
972+
} catch (Throwable ignored) {
973+
}
974+
}
975+
976+
/**
977+
* return a lighter color
978+
*
979+
* @param _factor percentage of light applied
980+
*/
981+
protected static int lighter(int color, float _factor) {
982+
983+
int red = (int) ((Color.red(color) * (1 - _factor) / 255 + _factor) * 255);
984+
int green = (int) ((Color.green(color) * (1 - _factor) / 255 + _factor) * 255);
985+
int blue = (int) ((Color.blue(color) * (1 - _factor) / 255 + _factor) * 255);
986+
return Color.argb(Color.alpha(color), red, green, blue);
987+
}
988+
989+
protected static boolean isLight(int color) {
990+
return Math.sqrt(
991+
Color.red(color) * Color.red(color) * .241 +
992+
Color.green(color) * Color.green(color) * .691 +
993+
Color.blue(color) * Color.blue(color) * .068) > 130;
994+
}
995+
996+
/**
997+
* adjust the alpha value of the color
998+
*
999+
* @return the color after adjustment
1000+
*/
1001+
protected static int adjustAlpha(int color, float _toAlpha) {
1002+
1003+
int alpha = Math.round(255 * _toAlpha);
1004+
int red = Color.red(color);
1005+
int green = Color.green(color);
1006+
int blue = Color.blue(color);
1007+
return Color.argb(alpha, red, green, blue);
1008+
}
9421009
}

textfieldboxes/src/main/java/studio/carbonylgroup/textfieldboxes/Utils.java

-111
This file was deleted.

0 commit comments

Comments
 (0)