just started messing with the android sdk and need to think about optimizing my code.
ive searched and done tutorials but it just doesnt click for togglebutton arrays.
this is an example of what i have, you can see if i have say 128 buttons its going to get messy.
i also need to know the checked state of the button
my activity class
private ToggleButton seqButton1;
private ToggleButton seqButton2;
blah.....
private ToggleButton seqButton128;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
seqButton1 = (ToggleButton) findViewById(R.id.btn1);
seqButton2 = (ToggleButton) findViewById(R.id.btn2);
....blah
any help or point in the right direction would be ace ta
edit:
this is my new code that crashes
package trkrPkg.trackr;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ToggleButton;
public class TrackerActivity extends Activity implements OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(this, "Toggle Button:" + buttonView.getId() + " is checked: " + isChecked, Toast.LENGTH_SHORT).show();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout)findViewById(R.id.my_toggle_container);
for (int i = 0; i<128; i++) {
ToggleButton tgl = new ToggleButton(this);
tgl.setId(i);
tgl.setOnCheckedChangeListener( this);
layout.addView(tgl);
}
}
}
You could add the ToggleButtons dynamically instead of declaring them in xml and