Skip to content

Instantly share code, notes, and snippets.

@victory316
Last active December 15, 2022 13:35
Show Gist options
  • Save victory316/cea0092c4a666d243c6fa182aee78aa1 to your computer and use it in GitHub Desktop.
Save victory316/cea0092c4a666d243c6fa182aee78aa1 to your computer and use it in GitHub Desktop.
CustomView template on Kotlin
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomView">
<attr name="customViewAtrribute" format="string" />
</declare-styleable>
</resources>
class CustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
private var binding: CustomViewBinding
private var typedArray: TypedArray
init {
binding = CustomViewBinding.inflate(LayoutInflater.from(context), this, true)
typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView)
}
}
<com.my.app.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:customViewAttribute="Hello world!"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment