Skip to content

Instantly share code, notes, and snippets.

@bitvale
Created November 22, 2018 20:12
Show Gist options
  • Select an option

  • Save bitvale/e12e9703ab09615a50e41c8a7ae2a110 to your computer and use it in GitHub Desktop.

Select an option

Save bitvale/e12e9703ab09615a50e41c8a7ae2a110 to your computer and use it in GitHub Desktop.
For Medium article "Android Dynamic Custom View is Easy"
class Switcher @JvmOverloads constructor(
context: Context,
attrs: AttributeSet ? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
init {
attrs?.let { retrieveAttributes(attrs, defStyleAttr) }
}
private fun retrieveAttributes(attrs: AttributeSet, defStyleAttr: Int) {
// retrieve cutom attributes
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
// setup switcher width and height
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
// setup helper sizes every time switcher size changed (radius, icon width...)
}
override fun onDraw(canvas: Canvas ?) {
// just draw
}
}
@konecnyna
Copy link

attrs?.let { retrieveAttributes(attrs, defStyleAttr) } should be attrs?.let { retrieveAttributes(it, defStyleAttr) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment