distante
distante

Reputation: 7025

How to define an interface in Kotlin that uses @DrawableRes annotations?

I want to create an interface to define a group of drawables needed for a mic state, something like:

interface MicrophoneState {
    val iconResource: Int
    val backgroundResource: Int
}

since it should be a resource id I wanted to decorated it with @DrawableRes but I get an error when I do that:

This annotation is not applicable to target 'member property without backing field or delegate'

Is there a way to fix that problem? all the question I have found are about classes but not interfaces.

Upvotes: 1

Views: 1165

Answers (2)

Anmol
Anmol

Reputation: 8670

As it's a state use of data class is ideal

data class MicrophoneState (
    @DrawableRes val iconResource: Int,
    @DrawableRes val backgroundResource: Int
)

or if you want to use interface

interface MicrophoneState {
    @DrawableRes fun iconResource(): Int
    @DrawableRes fun backgroundResource(): Int
}

Any one will work.

Update : I think you are looking for this exactly :

interface MicrophoneState {
    @get:DrawableRes val iconResource:Int
    @get:DrawableRes val backgroundResource:Int
}

Upvotes: 3

ror
ror

Reputation: 3500

I don't think you need an interface as apparently you're trying to build a model to store data?

class HelloWorldClass (@DrawableRes val value1: Int, @DrawableRes val value2: Int)

(you can as well go with data class)

Interface is a contract for something:

interface HelloWorldInterface {
    fun myMethod(@DrawableRes value: Int)
}

Upvotes: 0

Related Questions