0

Hey i tried to pass parameter in custom view class. It giving warning Custom view ChipTabView is missing constructor used by tools: (Context) or (Context,AttributeSet) or (Context,AttributeSet,int). I don't want to use suppressLint How to remove this warning can some one help me. I tried to this Custom view is missing constructor used by tools for adapter I don't want to change settings for android studio. I need proper solution how to achieve this in clean manner. Thanks in advance

enter image description here

class ChipTabView @JvmOverloads constructor(
    context: Context,
    private val number: Int,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127

2 Answers2

1

Create your own unique secondary constructor like this

class ChipTabView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private var number: Int = 0         

    constructor(number: Int, context: Context, attrs: AttributeSet? = null, defStyleAttribute: Int = 0) : this(context, attrs, defStyleAttribute) {
        this.number = number
    }
}
Ehma Ugbogo
  • 418
  • 5
  • 7
0

Remove the private val number: Int to make the constructor arguments conform to layout inflater requirements.

You can use a setter function to pass in values to your custom view.

laalto
  • 150,114
  • 66
  • 286
  • 303