1

I am working on a an Android Application . i have created some toggle buttons dynamically and they are clickable too...

what i want to achieve is toggle on any specific button and its ok. but when i toggle on any other button all other toggle button should go off..

like i can toggle on any one button at a time . if any other pressed on the previous one should go off.

there are dynamic number of buttons ..

and i dont know how to achieve this .

here is my code :

   for ( int i = 0; i<sez; i++  ){

    final ToggleButton btn = new ToggleButton(xxxxx.this);
       String g  = contactList.get(i).toString();
       Pattern p = Pattern.compile("\\{([^}]*)\\}");
       Matcher m = p.matcher(g);

       while (m.find()) {

       String[] po=m.group(1).split("=");
       btn.setId(i);
       btn.setTextOn("play");
       btn.setText(po[1]);
       btn.setTextOff(po[1]);

       final int id_ = btn.getId();
       Rowlayout layout = (org.xxxx.xxx.ui.Rowlayout) findViewById(R.id.adios);
       layout.addView(btn);

  btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
     Toast.makeText(InCallScreen.this,
    list2.get(id_) + "", Toast.LENGTH_SHORT).show();


                }
            });
  }
}

i have spent 3 days on it but still stuck in it, any one can help me . it will be much appreciated....

Addi.Star
  • 475
  • 2
  • 15
  • 36
  • 1
    Since you said it doesn't *have to be* ToggleButtons, have a look at RadioButton and RadioGroup : http://stackoverflow.com/questions/10107532/only-one-radio-button-selected-at-a-time – NSimon Apr 26 '16 at 16:02
  • i have tried this RadioButton . it was achieved but the requirement is now ToggleButton or simple Button .. because toggle button get two times listened when turning on and off – Addi.Star Apr 26 '16 at 16:07
  • @Addi.Star you can use `.setChecked(false)` method to set your buttons to Un checked in a for loop – KISHORE_ZE Apr 26 '16 at 16:25
  • will it only check only one toggle button at a time ? as i want to press other toggle button the previously pressed toggle button should go off – Addi.Star Apr 26 '16 at 16:28
  • @KISHORE_ZE : i have used the method but it goes with all the buttons turning them to be stay off state .. i want only on button to be on at a time and if any other button is pressed the previous pressed button should go off – Addi.Star Apr 27 '16 at 12:54
  • any help regarding this i tried puting them in radio group but it also dont allow me to trigger only one button at a time . and if next button is selected other pressed button should getCheked(false)... but it happens with all the button even for we curently pressing ... i need help in this searched all over the internet – Addi.Star Apr 27 '16 at 12:57
  • @Addi.Star you can put it in a for loop and toggle all the buttons except the current one off. You did mention that the number of buttons were dynamic right? – KISHORE_ZE Apr 27 '16 at 18:02
  • yes number of buttons are dynamic .. can you share me some code to get the idea of how to toggle off all other buttons except which one is pressed . because i tried doing this the button itself goes off and all the buttons start behaving like simple button not toggling.. – Addi.Star Apr 28 '16 at 08:58
  • Sorry I didn't get the notification. I will post code soon – KISHORE_ZE Apr 30 '16 at 02:58
  • @Addi.Star please see my answer. Sorry for the late answer I was offline for a couple of days and I didn't get the notification or open stack overflow. Anyways the code I gave worked for me. I hope it helps you! :) – KISHORE_ZE Apr 30 '16 at 05:19

1 Answers1

1

This code works perfectly for me. However I have removed a lot of your code to simplify the answer. So in trust you can modify those values I have set since I don't know the value of sez or the rowlayout I have replaced their values as sez = 10 and the layout to a linear layout. Anyways here is the code.

public class MainActivity extends Activity 
{
        int sez;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
            sez = 10;
            for ( int i = 0; i<sez; i++  ){

                    final ToggleButton btn = new ToggleButton(MainActivity.this);
                            btn.setId(i);
                            btn.setTextOn("play");
                            btn.setText("click");
                            btn.setTextOff("off");

                            final int id_ = btn.getId();
                            LinearLayout layout = (LinearLayout) findViewById(R.id.mainLinearLayout);
                            layout.addView(btn);

                            btn.setOnClickListener(new View.OnClickListener() {
                                            public void onClick(View view) {
                                                    Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
                                                    int buttonId = btn.getId();
                                                    for(int ii = 0; ii<sez; ii++)
                                                    {
                                                            if(ii!=buttonId)
                                                            {
                                                                    ToggleButton ButtonToOff = (ToggleButton)findViewById(ii);
                                                                    ButtonToOff.setChecked(false);
                                                            }
                                                    }
                                            }
                                    });
                    }
            }
 }

The part you probably have to add to your code is mainly in the onClick() method.

Hope in helped! :)

KISHORE_ZE
  • 1,466
  • 3
  • 16
  • 26
  • Sez the no of dynamic buttons.... anyhow i managed thanks a lot for your effort . it is a great answer – Addi.Star May 02 '16 at 10:24
  • I'm glad it helped you. Yes but I did not have the value of sez so I just used a random value for the sake of simplicity. :) – KISHORE_ZE May 02 '16 at 13:37
  • sez is just a variable getting the no of buttons to create . although it is fine with its value 10 .. anyone may can get help from this . thanks once again for your help . i accepted your answer .. :-) – Addi.Star May 02 '16 at 15:22
  • @Addi.Star Thank you and also sez can be any number. It does not matter. Sez can be any dynamic no. Probably not decimal or negative though... :) Let's go with a whole number. – KISHORE_ZE May 02 '16 at 18:02