Improve logic for setting box background mode.

Using a switch statement to ensure that the box background mode is only ever set to one of the BoxBackgroundMode constants. Also pulled the logic out into its own method to improve readability in the already large constructor, and added a check to make sure that the box background's GradientDrawable is only created if a background is requested.

PiperOrigin-RevId: 167194185
This commit is contained in:
afohrman 2017-08-31 17:44:59 -04:00 committed by Miguel Yanez Barreto
parent b84bf0b277
commit 7b4e6d356c
3 changed files with 24 additions and 9 deletions

View File

@ -321,7 +321,8 @@
<enum name="screen" value="15"/>
</attr>
<!-- Whether the text input box should be drawn in a filled or outline style. -->
<!-- Whether the text input box should be drawn with a filled background, an outline background, or no background.
This attribute is used internally, and it may be deleted if the implementation changes. -->
<attr name="boxBackgroundMode">
<enum name="none" value="0"/>
<enum name="filled" value="1"/>

View File

@ -65,6 +65,7 @@
</style>
<style name="Widget.Design.TextInputLayout" parent="android:Widget">
<item name="boxBackgroundMode">none</item>
<item name="hintTextAppearance">@style/TextAppearance.Design.Hint</item>
<item name="helperTextTextAppearance">@style/TextAppearance.Design.HelperText</item>
<item name="errorTextAppearance">@style/TextAppearance.Design.Error</item>

View File

@ -152,8 +152,8 @@ public class TextInputLayout extends LinearLayout {
private ColorStateList mBoxBackgroundColor;
/**
* Values for box background mode. There is either a filled background, an
* outline background, or no background.
* Values for box background mode. There is either a filled background, an outline background, or
* no background.
*/
@IntDef({BOX_BACKGROUND_NONE, BOX_BACKGROUND_FILLED, BOX_BACKGROUND_OUTLINE})
private @interface BoxBackgroundMode {}
@ -229,12 +229,10 @@ public class TextInputLayout extends LinearLayout {
defStyleAttr,
R.style.Widget_Design_TextInputLayout);
if (a.hasValue(R.styleable.TextInputLayout_boxBackgroundMode)) {
mBoxBackground = new GradientDrawable();
mBoxBackgroundMode =
a.getInt(R.styleable.TextInputLayout_boxBackgroundMode, BOX_BACKGROUND_NONE);
}
@BoxBackgroundMode
final int boxBackgroundMode =
a.getInt(R.styleable.TextInputLayout_boxBackgroundMode, BOX_BACKGROUND_NONE);
setBoxBackgroundMode(boxBackgroundMode);
mBoxPaddingOffsetPx =
context.getResources().getDimensionPixelOffset(R.dimen.design_textinput_box_offset);
@ -323,6 +321,19 @@ public class TextInputLayout extends LinearLayout {
}
}
/**
* Set the mode for the box's background (filled, outline, or none).
*
* @param boxBackgroundMode the box's background mode.
*/
private void setBoxBackgroundMode(@BoxBackgroundMode int boxBackgroundMode) {
if (boxBackgroundMode == mBoxBackgroundMode) {
return;
}
mBoxBackgroundMode = boxBackgroundMode;
mBoxBackground = mBoxBackgroundMode != BOX_BACKGROUND_NONE ? new GradientDrawable() : null;
}
/**
* Set the typeface to use for the hint and any label views (such as counter and error views).
*
@ -866,11 +877,13 @@ public class TextInputLayout extends LinearLayout {
case BOX_BACKGROUND_FILLED:
mBoxCornerRadius = 0f;
mBoxBackgroundColor = mDefaultTextColor;
mBoxStrokeWidth = 0;
break;
case BOX_BACKGROUND_OUTLINE:
mBoxCornerRadius = 16f;
mBoxStrokeColor = mFocusedTextColor;
mBoxBackgroundColor = null;
mBoxStrokeWidth = 7;
break;