Skip to content

Instantly share code, notes, and snippets.

View molidev8's full-sized avatar

Miguel Olivera molidev8

View GitHub Profile
@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)
@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 / 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 / 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 / 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 / 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 / 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 / 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 / customTrack.xml
Created November 17, 2022 19:05
A custom track for a Switch view
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:shape="rectangle">
<solid android:color="@color/blue_300" />
<corners android:radius="40dp" />
<size android:width="48dp" android:height="24dp" />
</shape>
</item>
<item android:state_checked="false">
@molidev8
molidev8 / customThumb.xml
Created November 17, 2022 19:02
A custom thumb for a Switch view
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="@color/blue_800" />
<size android:width="24dp" android:height="24dp" />
<stroke android:width="4dp" android:color="@android:color/transparent" />
</shape>
</item>