Skip to content

Instantly share code, notes, and snippets.

View molidev8's full-sized avatar

Miguel Olivera molidev8

View GitHub Profile
@molidev8
molidev8 / attrs.xml
Last active December 7, 2022 18:29
An attributes enumeration for a custom view
<declare-styleable name="CustomSwitch">
<attr name="switch_type" format="enum">
<enum name="vanilla" value="1" />
<enum name="small" value="2" />
<enum name="big" value="3" />
<enum name="pill" value="4" />
</attr>
<attr name="anim_text" format="boolean" />
<attr name="manual" format="boolean" />
@molidev8
molidev8 / main_activity.xml
Last active December 7, 2022 18:29
Using a custom view
<com.molidev8.customswitchsamples.view.customSwitch.CustomSwitch
android:id="@+id/myCustomSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:anim_text="true"
app:manual="true"
app:text="Bluetooth"
app:switch_type="pill"
app:textVisible="true" />
@molidev8
molidev8 / customViewConstructor.kt
Last active December 7, 2022 18:28
Overriding the constructor of a custom view
class CustomSwitch : ConstraintLayout {
private lateinit var manager: CustomSwitchManager
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
readAttrs(attrs)
}
@molidev8
molidev8 / CustomSwitchThumbPill.kt
Created December 7, 2022 19:01
A CustomView child
class CustomSwitchThumbPill : CustomSwitchManager {
private lateinit var binding: CustomSwitchThumbPillBinding
override fun initView(
context: Context,
viewGroup: ViewGroup,
animText: Boolean,
manual: Boolean,
textVisible: Boolean,
@molidev8
molidev8 / customSwitch.kt
Created December 7, 2022 19:02
CustomView implementation with Factory Pattern
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(
@molidev8
molidev8 / CustomSwitchFactory.kt
Created December 7, 2022 19:03
CustomSwitchFactory
object CustomSwitchFactory {
fun build(type: CustomSwitchType): CustomSwitchManager = when (type) {
CustomSwitchType.CustomThumbBigSwitch -> CustomSwitchThumbBig()
CustomSwitchType.CustomThumbSmallSwitch -> CustomSwitchThumbSmall()
CustomSwitchType.CustomTrackPillSwitch -> CustomSwitchThumbPill()
CustomSwitchType.Vanilla -> CustomSwitchVanilla()
}
}
@molidev8
molidev8 / CustomSwitchType.kt
Created December 7, 2022 19:04
CustomSwitchType to modelate the type
sealed class CustomSwitchType {
object Vanilla : CustomSwitchType()
object CustomThumbSmallSwitch : CustomSwitchType()
object CustomThumbBigSwitch : CustomSwitchType()
object CustomTrackPillSwitch : CustomSwitchType()
}
@molidev8
molidev8 / CustomSwitchManager.kt
Created December 7, 2022 19:05
CustomSwitchManager interface
interface CustomSwitchManager {
fun initView(
context: Context,
viewGroup: ViewGroup,
animText: Boolean,
manual: Boolean,
textVisible: Boolean,
text: String
)
fun setAnimText(shouldAnim: Boolean)