1

I have tried using the code mentioned in the link below to "mass disable" buttons and it works perfectly fine. However the same code does not work for mass enable.

Android: mass enable/disable buttons

Code for Disable (Working)

TableLayout tl = (TableLayout)findViewById(R.id.table1); // 
ArrayList<View> touchables = tl.getTouchables();
for(View touchable : touchables){
if( touchable instanceof Button && touchable != btnNewWord )
((Button)touchable).setEnabled(false);
}

CODE for Enable (Not Working)

btnNewWord.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

TableLayout tl = (TableLayout)findViewById(R.id.table1);  
ArrayList<View> touchables = tl.getTouchables();
for(View touchable : touchables){
if( touchable != btnNewWord )
((Button)touchable).setEnabled(true);
}                       
Community
  • 1
  • 1
  • Do you get any error or so? try to Log your ArrayList to check if it contains values once you disable the buttons and try to get ArrayList again?? – Hiral Vadodaria Oct 18 '11 at 06:06

1 Answers1

3

Once you set the buttons disabled,i think,they will no longer be touchable. So you need to modify that point in your code and use something else to get all views. You can preserve your ArrayList which you used to disable buttons and then use the same to re enable them.

EDIT :

Try this:

ArrayList<View> touchables //declare globaly

then

TableLayout tl = (TableLayout)findViewById(R.id.table1); // 
touchables = tl.getTouchables();
for(View touchable : touchables)
{
    if( touchable instanceof Button && touchable != btnNewWord )
      ((Button)touchable).setEnabled(false);
}

and now,

btnNewWord.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

       for(View touchable : touchables)
       {
          if( touchable != btnNewWord )
            ((Button)touchable).setEnabled(true);
       }  
   }
}                     
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • Actually I disable all except one button which is still enabled ie btnNewWord: if( touchable instanceof Button && touchable != btnNewWord ) I want the user to click that particular button to re-enable all buttons again. – Naheed Akhtar Oct 18 '11 at 08:09
  • That means,you have btnNewWord is the button which controls the disability of all other buttons.right? then you need to save all buttons(when they are enabled) in ArrayList as you did,and then you have to use the same ArrayList to re enable button in onClick() of btnNewWord. – Hiral Vadodaria Oct 18 '11 at 09:00
  • Thanks a lot for your quick response and effort to put the explanation across to me. Though the code still isn't working I can now understand the problem and am trying to get a workaround. – Naheed Akhtar Oct 18 '11 at 14:43
  • Thanks to all ...it worked after i followed the inputs from Hiral – Naheed Akhtar Oct 20 '11 at 16:43