2

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

0

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is the "it" meant to be replaced with anything? Like a counter? If I use the code as per your suggestion "enabled" reference does not resolve any longer
@Karl Griffiths please see my updated answer, sorry used the wrong property, should be isEnabled and not enabled
doh! I should have noticed that as well. Thank you. Also you are right this is a loop like any other and ultimately what I was describing as needed, I had a much more long winded view of the loop in my head than would have been worth it which is what I wanted to avoid. I had no idea I could write it so simply. Thank you for your assistance
@Karl Griffiths no problem mate, and Kotlin is a very "sexy" language syntactically in my opinion when compared to Java and even C#. But yeah as you learn the language more it will grow on you even further 😄

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.