Search code examples
androidviewsdkandroid-custom-view

Call requires API level 21 (current min is 19): android.view.View()


So I want to create a library that use custom view. I want to inherit View class, but it's error because my minsdk is 19. I need my library to support sdk 19. Is there a way to solve this?

class Dummy() :  View(context, attrs, defStyleAttr, defStyleRes) {

Solution

  • You should use another constructor for this. The one you used is available only from the API 21.

    For custom views I use it like this:

    class Dummy @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : View(context, attrs, defStyleAttr)
    

    So 2 things here:

    1. Use View constructor with 3 params. Context, attrs, defStyleAttr.
    2. Use @JvmOverloads annotation and default values for parameters to support actually 3 constructors in one line.