Kotlin button array

Viewed 73

I really hope this question has not been answered everywhere else already but every search seems to focus on listeners and other uses of a button array but i want to also use it for formatting all buttons at the same time (activate, deactivate etc)

So here is what I have tried;

val buttons = arrayOf(btn1,btn2,btn3,btn4)

This will work, BUT will only change a single button

buttons[0].isEnabled=true; // 

Then this is the bit that I am struggling with;

buttons[0..buttons.size].isEnabled=true;

The response is basically that it expects a single number and not a range.

I also tried;

buttons[].isEnabled=true;

The response is that it requires an index

I also tried

buttons.isEnabled=true;

This of course will not resolve properly

My key question really is can I apply formatting/state changes to all using an array or will I always have to do it for each button in turn?

I think it would be possible to create a loop but that isn't the route I wanted to follow here if there is an alternative

1 Answers

Don't think there is a way mate. You gotta loop and regardless of what syntatic sugar a language has in the end its still a for loop.

You could do:

buttons.forEach {
    it.isEnabled = true
}
Related