Hi, recently i was learning on how to make custom ...
# android
k
Hi, recently i was learning on how to make custom views and i'm found out of the resources which using the method to inflate other views(from other xml activity). after trying it out to referencing the custom views in
activity_main.xml
in form of
<com.test_project.library.buttons.TestButton/>
, it did works perfectly, but i found out that i cannot override the text value because the
android:text
doesn't appear from suggestion. is this because the effect from inflating the views? or there is something that missing from the custom views class that i didn't declare it first so i cannot put text value in
activity_main.xml
?
😶 1
the code of the custom views class
Copy code
class TestButton : RelativeLayout {

    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attr: AttributeSet? = null) : super(context, attr) {
        initView()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int
    ) : super(context, attrs, defStyleAttr) {
        initView()
    }

    private fun initView() {
        val rootView = (context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)
            .inflate(R.layout.activity_button_default, this, true)
    }
}
c
The
android:text
will only be available, by default, if your custom view is a subclass of
TextView
. In your case, you extend from the
RelativeLayout
thus have no access to the xml attributes of the TextView. 1 way to fix this is by adding custom attributes to your custom view. See this post for more information. https://medium.com/@Zielony/guide-to-android-custom-views-attributes-ab28de3e54b7
👍 1
k
sorry for late reply, thank you for the reference, i've tried to experimenting with it and it works perfectly just like what i wanted.