I have a ToggleButton and I need to set up simple click actions. How do I implement a simple click listener for a ToggleButton?
If you need details please ask.
I have a ToggleButton and I need to set up simple click actions. How do I implement a simple click listener for a ToggleButton?
If you need details please ask.
    this.someToggleButton = (ToggleButton)findViewById(R.id.someToggleButton) ;
    this.someToggleButton.setOnCheckedChangeListener( new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
            doSomethingWith(toggleButton, isChecked) ;
        }
    }) ;
 
    
    ToggleButton extends View, so you can simply use View.setOnClickListener(), like this:
// get your ToggleButton
ToggleButton b = (ToggleButton) findViewById(R.id.myButton);
// attach an OnClickListener
b.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        // your click actions go here
    }
});
 
    
    Use View.setOnClickListener() and Check state of button.
    final ToggleButton tB = (ToggleButton) findViewById(R.id.toggleButton1);
    tB.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if(tB.isChecked()){
                //Button is ON
                            // Do Something 
            }
            else
            //Button is OFF
                            // Do Something     
        }
    });
 
    
    Just to add a point not emphasised in the other answers - programatically binding a click handler is a bit heavy on the bolierplate code. As mentioned in the docs, it's only necessary in certain scenarios, such as:
If the ToggleButton is defined in the layout, it's far simpler and cleaner to bind a handler method there
<ToggleButton 
  android:id="@+id/togglebutton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textOn="On"
  android:textOff="Off"
  android:onClick="onToggleClicked"/>
Then only the handler method needs to be defined in the Activity Java
public void onToggleClicked(View view) {
    if(((ToggleButton) view).isChecked()) {
        // handle toggle on
    } else {
       // handle toggle off
    }    
}
Note the method can have any name, but the signature must meet these criteria:
public methodvoidView (this will be the View which was clicked) 
    
    mTB = (ToggleButton) findViewById(R.id.toggleButton1);
mTB.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // Is the toggle on?
            boolean on = ((ToggleButton) v).isChecked();
            if (on) {
                // Enable here
            } else {
                // Disable here
            }
        }
    });
 
    
    @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked){
        Toast.makeText(getApplicationContext(),"on",Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(getApplicationContext(),"off",Toast.LENGTH_SHORT).show();
    }
}
 
    
     
    
    To add it from the code, you can do something like:
yourButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      finish();
    }
  });
However, you can also specify in the XML for your button, which method you want to be associated with the onClick action/event.
 
    
    Try setOnCheckedChangeListener method of your ToggleButton class
ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
        // 
    }
}) ;
 
    
    if above codes don't work try
b.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
       // your click actions go here
    }
});
