Skip to content

Instantly share code, notes, and snippets.

@Kaspic
Last active February 9, 2021 21:01
Show Gist options
  • Save Kaspic/90f5545bbbd449e38e76c0c772c8e2f6 to your computer and use it in GitHub Desktop.
Save Kaspic/90f5545bbbd449e38e76c0c772c8e2f6 to your computer and use it in GitHub Desktop.
class TutorialCustomView(context: Context, attrs: AttributeSet): FrameLayout(context, attrs) {
init {
inflate(context, R.layout.tutorial_custom_view, this)
val mainText : TextView = findViewById(R.id.main_text)
context.withStyledAttributes(attrs, R.styleable.TutorialCustomView) {
mainText.apply {
setBackgroundColor(getColorOrThrow(R.styleable.TutorialCustomView_mainColor))
text = getString(R.styleable.TutorialCustomView_mainText)
}
}
}
}
class TutorialCustomView(context: Context, attrs: AttributeSet): FrameLayout(context, attrs) {
init {
inflate(context, R.layout.tutorial_custom_view, this)
val mainText : TextView = findViewById(R.id.main_text)
val attributesTypedArray = context.obtainStyledAttributes(attrs, R.styleable.TutorialCustomView)
mainText.apply {
setBackgroundColor(attributesTypedArray.getColorOrThrow(R.styleable.TutorialCustomView_mainColor))
text = attributesTypedArray.getString(R.styleable.TutorialCustomView_mainText)
}
attributesTypedArray.recycle()
}
}
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="16dp"/>
</merge>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TutorialCustomView">
<attr name="mainText" format="string"/>
<attr name="mainColor" format="color"/>
</declare-styleable>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment