Kotlin when with multiple values not working when value is an android view

Viewed 33272

I implemented a function that is used in anko's apply recursively:

fun applyTemplateViewStyles(view: View) {
    when(view) {
        is EditText, TextView -> {
            ....
        }
    }
}

And I receive an error saying that "Function invocation 'TextView(...)' expected"

Since I can write an when with a clause like is 0, 1, why I can't do the same with an Android View?

4 Answers

In case of multiple text option handling you can use comma

when(option) { //option is string
    "type A","type B" -> {
        ....
    }
}

Use comma-separated to handle multiple options for the same execution.

{ 
view ->
   when(view.id) {
       homeView.tv_new_dealer_rank_to_achieve.id,
       homeView.tv_sales_rank_to_achieve.id,
       homeView.tv_payment_rank_to_achieve.id,
       homeView.tv_bill_dealer_rank_to_achieve.id -> {
           homePresenter.reDirectToFragment(10)
       }
    }
}
Related