Search code examples
androidbuttonseekbar

Dynamically created button not working for created seekbar


This is my code, I want the created toggle buttons to set the position of the created seekbar to 100/0, however only the last toggle button created is able to set the last seekbar's position. I have created the buttons and seekbar and have assigned ids to them using for loop. Thanks for the help.

public class MainActivity extends Activity {

TableLayout tl = null;
SeekBar seekbar = null;
TextView t = null;
ToggleButton bt = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tl = (TableLayout)findViewById(R.id.lightslist);
    t = (TextView)findViewById(R.id.progress);

    for(int i = 1; i <= 20; i++){

         TableRow tr = new TableRow(this);

         seekbar = new SeekBar(this);
         seekbar.setMax(100);
         seekbar.setId(i);
         seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                public void onStopTrackingTouch(SeekBar sb) {
                    // TODO Auto-generated method stub
                    int progress = sb.getProgress();
                    t.setText(sb.getId() + " at " + progress);

                }

                public void onStartTrackingTouch(SeekBar sb) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getBaseContext(), "Start", Toast.LENGTH_SHORT).show();
                }

                public void onProgressChanged(SeekBar sb, int progress, boolean a) {
                    // TODO Auto-generated method stub

                }

            });

            TableRow.LayoutParams ob = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT,
                    1);

            bt = new ToggleButton(this);
            bt.setId(i + 100);

            bt.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {

                    if (bt.isChecked())
                    {
                             seekbar.setProgress(100);
                    }else{
                        seekbar.setProgress(0);
                    }
                }

          });


            tr.addView(bt);

            tr.addView(seekbar, ob);

            tl.addView(tr);

    }

}

Solution

  • Your seekbar gets reassigned every time in the for loop. So the value of the last created Seekbar is the one finally available in the member variable.

    Instead of the member variable, you can create a final variable and assign the dynamically created instance to it.

    final SeekBar seekbar = new SeekBar(this);
    

    OR

    You could set the seekbar as the tag of the ToggleButton and retrieve the tag in the onClick method.

            bt = new ToggleButton(this);
            bt.setId(i + 100);
            bt.setTag(seekbar);
    
            bt.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
    
                    if (bt.isChecked())
                    {
                             SeekBar sb = (SeekBar)getTag();
                             sb.setProgress(100);
                    }else{
                        sb.setProgress(0);
                    }
                }
    
          });