0

Here is my code

  theme?.let {
  ....
    titleText.setTextColor(it.pTextColor)
    contentText.setTextColor(it.pTextColor)
  }
}

and the usage of this extension is here cardView.setTheme(theme)

How can I have this cardView.theme = theme

Artur A
  • 257
  • 3
  • 20
  • I don't get your question. You haven't posted the definition of this extension function `cardView.setTheme(theme)` and `cardView.theme = theme` that would be an extension property not a function, see https://kotlinlang.org/docs/reference/extensions.html. Please clarify. – donfuxx Jul 14 '19 at 18:48
  • Thanks @donfuxx yes I forgot about extension properties it is what I want to do , but as I understand from the doc you posted initializers are not allowed for extension properties, so I can't do what I wanted to do yes? – Artur A Jul 14 '19 at 20:46

1 Answers1

2

you can write extension function:

fun CardView.setTheme(theme: Theme) {
    val titleText = findViewById<TextView>(R.id.text)
    val contentText = findViewById<TextView>(R.id.content)
    titleText.setTextColor(theme.pTextColor)
    contentText.setTextColor(theme.pTextColor)
}

or extension property:

var CardView.theme: Theme?
    get() = null
    set(value) {
        value ?: return
        val titleText = findViewById<TextView>(R.id.text)
        val contentText = findViewById<TextView>(R.id.content)
        titleText.setTextColor(value.pTextColor)
        contentText.setTextColor(value.pTextColor)
    }
Andrei Tanana
  • 7,932
  • 1
  • 27
  • 36