7

I have tried the code in the answer of android-widget-switch-on-off-event-listener, but the post doesn't say anything about the error I got trying to use it.

At the second line of the suggested code:

    switch1 = (Switch) findViewById(R.id.switch1);

    switch1.setOnCheckedChangeListener(new OnCheckedChangedListener() { //This line has the error
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            aTextView.setText("Switch was toggled");
        }
    });

This error triggers

The method setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener) in the type CompoundButton is not applicable for the arguments (new OnCheckedChangedListener(){})

How can I fix this? All I want to do is call a function when the switch changes - as opposed to when it is clicked. Thanks.

Community
  • 1
  • 1
kevbot
  • 453
  • 1
  • 4
  • 13

2 Answers2

11

Set the listener to this because your class implements compoundbutton like so...

switch1.setOnCheckedChangeListener(this);

Then add this method in your code...

    @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            boolean = isChecked;
            //whatever you want
        }

EDIT: if you havent implemented CompoundButton.OnCheckedChangedListener use this...

switch.setOnCheckedChangeListener(new OnCheckedChangeListener(

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub

            }

        });
Ogen
  • 6,499
  • 7
  • 58
  • 124
  • It still doesn't like that, it says "The method onCheckChangedListener(new onCheckChangedListener(){}) is undefined for the type Switch" does it matter that it is the type of switch available from api 14? – kevbot Jan 09 '14 at 03:09
  • @grevin123 yeah that was wrong, see my new answer i updated it – Ogen Jan 09 '14 at 03:10
  • Still not working, I must be putting it in the wrong place. Does your updated method go in any special place? – kevbot Jan 09 '14 at 03:22
  • @grevin123 Ok wait, does your class implement CompoundButton.OnCheckedChangeListener? – Ogen Jan 09 '14 at 03:23
  • Yes, it then wants to make my activity abstract and it doesn't seem to help, is it OnCheckChacngedListener or OnCheckedChangeListener? Sorry, I'm very new to android dev – kevbot Jan 09 '14 at 03:28
  • @grevin123 its onCheckedChangeListener – Ogen Jan 09 '14 at 03:46
  • please follow the link http://custom-android-dn.blogspot.in/2013/01/how-to-use-and-custom-switch-in-android.html – Jitesh Upadhyay Jan 09 '14 at 03:48
0

Hi please have a look at http://custom-android-dn.blogspot.in/2013/01/how-to-use-and-custom-switch-in-android.html

we can do this in given way

 switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
                if (buttonView.isChecked()){
                                               //do something   }
                else{
                    //do something 
                }
            }
    });
Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43