Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some attributes for further customization when the edittext is disabled. #267

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public class MaterialEditText extends AppCompatEditText {
*/
private int errorColor;

/**
* the color for the disabled dotted underline.
*/
private int disabledUnderlineColor;

/**
* min characters count limit. 0 means no limit. default is 0. NOTE: the character counter will increase the View's height.
*/
Expand Down Expand Up @@ -304,6 +309,7 @@ public class MaterialEditText extends AppCompatEditText {
private ColorStateList textColorStateList;
private ColorStateList textColorHintStateList;
private ArgbEvaluator focusEvaluator = new ArgbEvaluator();
private boolean dottedBottomLinesForDisabledState;
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
StaticLayout textLayout;
Expand Down Expand Up @@ -378,8 +384,10 @@ private void init(Context context, AttributeSet attrs) {
primaryColor = typedArray.getColor(R.styleable.MaterialEditText_met_primaryColor, defaultPrimaryColor);
setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_met_floatingLabel, 0));
errorColor = typedArray.getColor(R.styleable.MaterialEditText_met_errorColor, Color.parseColor("#e7492E"));
disabledUnderlineColor = typedArray.getColor(R.styleable.MaterialEditText_met_disabledBottomLineColor, -1);
minCharacters = typedArray.getInt(R.styleable.MaterialEditText_met_minCharacters, 0);
maxCharacters = typedArray.getInt(R.styleable.MaterialEditText_met_maxCharacters, 0);
dottedBottomLinesForDisabledState = typedArray.getBoolean(R.styleable.MaterialEditText_met_disabledBottomLineDotted, true);
singleLineEllipsis = typedArray.getBoolean(R.styleable.MaterialEditText_met_singleLineEllipsis, false);
helperText = typedArray.getString(R.styleable.MaterialEditText_met_helperText);
helperTextColor = typedArray.getColor(R.styleable.MaterialEditText_met_helperTextColor, -1);
Expand Down Expand Up @@ -1316,11 +1324,21 @@ protected void onDraw(@NonNull Canvas canvas) {
paint.setColor(errorColor);
canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint);
} else if (!isEnabled()) { // disabled
paint.setColor(underlineColor != -1 ? underlineColor : baseColor & 0x00ffffff | 0x44000000);
float interval = getPixel(1);
for (float xOffset = 0; xOffset < getWidth(); xOffset += interval * 3) {
canvas.drawRect(startX + xOffset, lineStartY, startX + xOffset + interval, lineStartY + getPixel(1), paint);
int disabledLineColor = disabledUnderlineColor;
if (disabledUnderlineColor == -1){
disabledLineColor = underlineColor != -1 ? underlineColor : baseColor;
}
paint.setColor(disabledLineColor & 0x00ffffff | 0x44000000);
if (dottedBottomLinesForDisabledState){
float interval = getPixel(1);
for (float xOffset = 0; xOffset < getWidth(); xOffset += interval * 3) {
canvas.drawRect(startX + xOffset, lineStartY, startX + xOffset + interval, lineStartY + getPixel(1), paint);
}
}
else{
canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(1), paint);
}

} else if (hasFocus()) { // focused
paint.setColor(primaryColor);
canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint);
Expand Down
4 changes: 4 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<attr name="met_minCharacters" format="integer" />
<!-- max Characters count limit. 0 means no limit. -->
<attr name="met_maxCharacters" format="integer" />
<!-- If disabled, then the bottom line is dotted if true. Defaults to true. -->
<attr name="met_disabledBottomLineDotted" format="boolean" />
<!-- The color for when something is wrong.(e.g. exceeding max characters) -->
<attr name="met_disabledBottomLineColor" format="color" />
<!-- Whether to show the bottom ellipsis in singleLine mode -->
<attr name="met_singleLineEllipsis" format="boolean" />
<!-- Reserved bottom text lines count, no matter if there is some helper/error text. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ protected void onCreate(Bundle savedInstanceState) {
initSingleLineEllipsisEt();
initSetErrorEt();
initValidationEt();

findViewById(R.id.disabledSolidLine).setEnabled(false);
}

private void initEnableBt() {
Expand Down
9 changes: 9 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,15 @@
app:met_iconPadding="0dp"
app:met_maxCharacters="5" />

<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/disabledSolidLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Solid line disabled color"
android:text="This is disabled and still shows a solid line."
app:met_disabledBottomLineColor="#33ff0000"
app:met_disabledBottomLineDotted="false"/>

</LinearLayout>
</LinearLayout>
</ScrollView>