Tuesday, 12 February 2013

Set Listener for ToggleButton in Android ?


ToggleButton toggleButton;

            // Get the Reference for ToggleButton
            toggleButton = (ToggleButton) findViewById(R.id.toggleButton);

           
            // set Listener for ToggleButton (CheckedListener)
            toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

                        if (isChecked) {
                              Toast.makeText(getApplicationContext(),
                                          "ToggleButton Staus ==> ON", Toast.LENGTH_LONG)
                                          .show();
                        } else {
                              Toast.makeText(getApplicationContext(),
                                          "ToggleButton Staus ==> OFF", Toast.LENGTH_LONG)
                                          .show();
                        }
                  }
            });

No comments:

Post a Comment