39

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.

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
nkcmr
  • 10,690
  • 25
  • 63
  • 84

9 Answers9

56
    this.someToggleButton = (ToggleButton)findViewById(R.id.someToggleButton) ;
    this.someToggleButton.setOnCheckedChangeListener( new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
            doSomethingWith(toggleButton, isChecked) ;
        }
    }) ;
Keith John Hutchison
  • 4,955
  • 11
  • 46
  • 64
  • 6
    I tried this first, but in my case the `OnClickListener` was actually the correct route to go, because `OnCheckedChangedListener` gets fired even when you change `isChecked` programmatically. That was causing undesired side effects when I first loaded the activity and set the initial checked state. – jokeefe Jan 03 '14 at 18:19
  • 1
    @jokeefe Could you not set the initial checked state before setting the `onCheckedChangedListener`? – Masterfool Jun 16 '15 at 22:32
41

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
    }
});
Jacob Ras
  • 5,974
  • 1
  • 28
  • 26
  • if you haven't imported view . you can use it likeb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // your click actions go here } }); – Gihan Aug 16 '14 at 20:24
33

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     
        }
    });
salman khalid
  • 4,884
  • 4
  • 27
  • 33
17

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)
davnicwil
  • 28,487
  • 16
  • 107
  • 123
2
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
            }

        }
    });
AbhinayMe
  • 2,399
  • 1
  • 20
  • 21
2
@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();
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Arbaz Pirwani
  • 935
  • 7
  • 22
  • 1
    When posting an answer, try to explain why this fixes this issue, along with the code. – David Oct 04 '17 at 11:04
2

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.

Luis Miguel Serrano
  • 5,029
  • 2
  • 41
  • 41
1

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) {
        // 
    }
}) ;
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
1

if above codes don't work try

b.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
       // your click actions go here
    }
});
Jayson Tamayo
  • 2,741
  • 3
  • 49
  • 76