Android: mass enable/disable buttons
Asked Answered
D

3

6

I have an activity where a bunch of buttons are placed inside TableLayout, not unlike a dial pad. During some operations I need to temporarily disable the buttons. To my unpleasant surprise doing TableLayout.setEnabled(false) has no effect on the nested buttons. Am I stuck with setting each individual button or is there a nifty (better) way to achieve the same?

Deragon answered 8/7, 2010 at 15:41 Comment(0)
R
9

I'd try to do something like this:

TableLayout tableLayoutInstance; // let's suppouse you have already initialized it
// blablabla
// example to deactivate all buttons
ArrayList<View> touchables = tableLayoutInstance.getTouchables();
for(View touchable : touchables){
    if( touchable instanceof Button )
        ((Button)touchable).setEnabled(false);
}
Registered answered 8/7, 2010 at 15:53 Comment(2)
Not ideal but acceptable in my circumstances (and short enough). Thanks!Deragon
((Button)touchable).setEnabled(false);Abecedarium
I
1

I think you have to set each individual of this Buttons to deactivated. To make it look a little bit nicer you could put all of the buttons in a list and iterate over them during activating and deactivating. But this will not prevent you from finding them all once in you code.

Ierna answered 8/7, 2010 at 15:44 Comment(1)
At very least I can create a custom button which listens on certain event and enables/disables based on that. I'm just trying to be lazy before I go that route :)Deragon
E
0

Since the buttons are nested under a TableLayout, it should be easy to iterate over the children and set each one. I don't know if there is an easier way.

Ebby answered 8/7, 2010 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.