Last active
December 7, 2022 18:28
-
-
Save molidev8/7a9f9809a85115d2a811a958bbaf86f1 to your computer and use it in GitHub Desktop.
Overriding the constructor of a custom view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomSwitch : ConstraintLayout { | |
private lateinit var manager: CustomSwitchManager | |
constructor(context: Context) : super(context) | |
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { | |
readAttrs(attrs) | |
} | |
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super( | |
context, | |
attrs, | |
defStyleAttr | |
) { | |
readAttrs(attrs) | |
} | |
private fun readAttrs(attrs: AttributeSet) { | |
with(context.theme.obtainStyledAttributes(attrs, R.styleable.CustomSwitch, 0, 0)) { | |
inflateView( | |
getSwitchTypeFromAttr(this), | |
getBoolean(R.styleable.CustomSwitch_anim_text, false), | |
getBoolean(R.styleable.CustomSwitch_manual, false), | |
getBoolean(R.styleable.CustomSwitch_textVisible, false), | |
getString(R.styleable.CustomSwitch_text).orEmpty() | |
) | |
recycle() | |
} | |
} | |
private fun getSwitchTypeFromAttr(attributes: TypedArray): CustomSwitchType = | |
when (attributes.getInt(R.styleable.CustomSwitch_switch_type, 1)) { | |
1 -> Vanilla | |
2 -> CustomThumbSmallSwitch | |
3 -> CustomThumbBigSwitch | |
else -> CustomTrackPillSwitch | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment