Toggle Button
In OnCreate
ToggleButton tb = new ToggleButton(this); tb.setId(1); linear2.addView(tb);
final ToggleButton tbc = (ToggleButton)findViewById(1);
tb.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (tbc.isChecked()) {
textview1.setText("ON");
} else {
textview1.setText("OFF");
} }});
Video Help