get state of ToggleButton through handler
Asked Answered
G

2

24

I need to be able to access the state of a ToggleButton seeing is how there is no way to create methods for the specific state of a ToggleButton. So here is where I'm at so far:

ToggleButton syncSwitch = (ToggleButton)findViewById(R.id.toggleButton1);
syncSwitch.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View view){
        Toast.makeText(getApplicationContext(), "Click!", Toast.LENGTH_SHORT).show();
    }
});

Now all I need is some type of boolean method that can tell the handler the state of the ToggleButton.

Garygarza answered 22/8, 2011 at 2:12 Comment(2)
syncSwitch.isChecked() not helping?Archilochus
see my comment on Nikolay's answerGarygarza
F
45

What do you mean by 'create methods for the specific state'? You can get the state using isChecked().

Falsework answered 22/8, 2011 at 2:18 Comment(4)
okay, thats weird. you are right but I've been up and down the ToggleButton docs and I didn't see that.Garygarza
Sometimes methods are hidden because are inherited from other classes. Anyway that documentation really sucks indeed, isChecked() is under android.widget.CompoundButtonArchilochus
I wouldn't go so far as to say it sucks. You just need to click the 'Inherited methods' link, and there is a very aptly named 'Checkable' interface in the list. Details are hidden by default, but that's a feature since the inheritance hierarchy for UI widgets can be pretty deep.Falsework
@Archilochus If the document had every inherited method listed as you say then I'm probably you'll complain again it sucks because methods just repeat in just about every class.Drin
M
14

You can also check by using togglebtn.setOnCheckedChangeListener Listener like this:

ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(getApplicationContext(),
                        String.valueOf(buttonView.isChecked()), Toast.LENGTH_SHORT).show();
            }
        });
Matter answered 22/8, 2011 at 2:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.