views:

251

answers:

2

I'm creating buttons dynamically ...

for(int i=0; i<colSize;i++){
    final Button btn = new Button(this);    
    btn.setText(SectionName[i]);        
    btn.setTextSize(10);
    btn.setPadding(8, 3,8, 3);   
    btn.setTextColor(Color.WHITE);
    btn.setTypeface(Typeface.SERIF, Typeface.BOLD);

    btn.setOnClickListener(new OnClickListener() {  
        @Override
        public void onClick(View v) {               
            //***Every time that I click my button is selected !:)
            btn.setSelected(true);      
        }                   

     });  

    }

But how could I deselect the other buttons that were selected, I just want one Button selected! :)

+3  A: 
  1. The brutal way (works if you have few buttons) - save your button references and create private method which loops through your buttons and deselects once you don't need
  2. Extend your button class and make it listen for custom event which is generated when one of the buttons is clicked
  3. Look at the RadioGroup implementation
  4. Variation of #1. Instead of creating separate listeners for your buttons create just one and reuse it for all buttons. Extend that listener from OnClickListener and add List field. Each time you assign listener to the button add button reference to that list. Now, when onClick is triggered simply loop through the list and disable "other" buttons
DroidIn.net
To me, it sounds like he just needs to use RadioGroup.
Daniel Lew
Agreed but will it work with push buttons?
DroidIn.net
No, it requires a RadioButton, but that's the more standard UI element to use for this situation. Otherwise, your other suggestions are good.
Christopher
hi Guys thanks for your Help!!! Im doing a menu with custom buttons http://www.gersic.com/blog.php?id=56inside a HorizontalScrollview!
+1  A: 

Declare a variable to store the Id of the Clicked Button ::

private int EnabledButton;

set an ID on every button when are created ::

btn.setId(i);

or a tag ::

btn.setTag(i);   

then in that Listener get the "EnabledButton", and call a function to deselect the other buttons::

 btn.setOnClickListener(new OnClickListener() {  
        @Override
        public void onClick(View v) {                 
        EnabledButton=btn.getId();
        DeselectButtons();        
        btn.setSelected(true);      
    }                   

 });  

The Function to deselect the other Buttons ::

public void DeselectButtons() {
    for(int i=0; i<NumberofButtons;i++){            
                    if (EnabledButton!= i)
        this.findViewById(i).setSelected(false);
    }           

}
Jorgesys
With this approach, you don't even need the `enabledButton` variable as you're setting the selected button after deselecting others. But anyway, your `deselectButtons` method won't work as you're not referencing the actual resource ID -- you're just looping from 1 to n. Just store a reference to each `Button` in an array and then you can loop over that. Or try the `RadioGroup` solution DroidIn mentioned above.
Christopher
wow! thank you very much Jorges I needed only buttons in a Horizontal scrollview like a tabstrip control in iphone, it works!!! thank you very much!!!!!!!!!!
well certainly you could use Button[] setOfButtons;
Jorgesys