Last active
February 9, 2021 21:01
-
-
Save Kaspic/90f5545bbbd449e38e76c0c772c8e2f6 to your computer and use it in GitHub Desktop.
withStyledAttributes extension function tutorial - https://kennay-kermani.medium.com/android-kotlin-resolve-custom-views-attributes-with-style-f29d5084e9fb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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