Home » Android » android – attach onClickListener to ToggleButton

android – attach onClickListener to ToggleButton

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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
    }
});

Answer:

    this.someToggleButton = (ToggleButton)findViewById(R.id.someToggleButton) ;
    this.someToggleButton.setOnCheckedChangeListener( new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
            doSomethingWith(toggleButton, isChecked) ;
        }
    }) ;

Answer:

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     
        }
    });

Answer:

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 instantiated at runtime
  • If the click behaviour is defined in a Fragment subclass

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:

  • Must be a public method
  • Must return void
  • Must take a single argument of type View (this will be the View which was clicked)

Answer:

@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();
    }
}

Answer:

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.

Answer:

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
            }

        }
    });

Answer:

if above codes don’t work try

b.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
       // your click actions go here
    }
});

Answer:

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) {
        // 
    }
}) ;