1

Is it possible to do that? I have following code:

class MyButton : AppCompatButton {
    @JvmOverloads
    constructor(
        context: Context,
        attrs: AttributeSet? = null,
    defStyleAttr: Int = 0)
    : super(context, attrs, defStyleAttr) {
        inflateView(attrs)
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(
        context: Context,
        attrs: AttributeSet?,
    defStyleAttr: Int,
    defStyleRes: Int)
    : super(context, attrs, defStyleAttr, defStyleRes) {
        inflateView(attrs)
    }

    fun inflateView(attrs: AttributeSet?) {
        LayoutInflater.from(context).inflate(R.layout.my_button, this, true)
    }
}

The inflate function gives me this error:

enter image description here

I understand that AppCompatButton is not inherited from ViewGroup, but what is the correct function?

Rendy
  • 5,572
  • 15
  • 52
  • 95
  • It's not clear what you are trying to do. Is it just a custom style for button? – Ircover Feb 13 '20 at 07:29
  • Actually yes, Is it better to do in code level @Ircover? – Rendy Feb 13 '20 at 07:30
  • I can't say it is a good practice to mention particular style in code, while it should be replaceable in xml layout or app style configuration. So I recommend to look [this topic](https://stackoverflow.com/a/29848987/4762282). – Ircover Feb 13 '20 at 07:33
  • Yes I am aware of that. I have been doing that for Android Java. So I am looking how to do it in Kotlin. – Rendy Feb 13 '20 at 07:35

0 Answers0