Reputation: 1600
The issue I am having is managing the states of a togglebutton being on and off. The button turns bluetooth on/off. How can I make it so the button can be toggled readily on and off without closing the app? Right now it only works when launching the application.
if(!myBluetooth.isEnabled()){
final ToggleButton tglbtn = (ToggleButton)findViewById(R.id.ToggleButton01);
tglbtn.setChecked(false);
tglbtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
myBluetooth.enable();}});
}
else {
ToggleButton tglbtn = (ToggleButton)findViewById(R.id.ToggleButton01);
tglbtn.setChecked(true);
tglbtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
myBluetooth.disable();}});
}
I tried implementing a loop to accomplish this, but the app kept crashing, the only other thing I could think of is a goto
down at the bottom below the two myBluetooth instance's, linking to the other one but since that's not possible in java, I'm out of ideas.
while(myBluetooth != null){
tglbtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
myBluetooth.enable();
toggleflag = true; }});
while (toggleflag){
tglbtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
myBluetooth.disable();
toggleflag = false; }});
}
}
Upvotes: 0
Views: 337
Reputation: 1751
Try This
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getApplicationContext(), buttonView.isChecked()+"",Toast.LENGTH_SHORT).show();
}
});
Upvotes: 1