Adnan Quraishi
Adnan Quraishi

Reputation: 533

One Toggle Button on at a time

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....

Upvotes: 1

Views: 980

Answers (1)

KISHORE_ZE
KISHORE_ZE

Reputation: 1466

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! :)

Upvotes: 1

Related Questions