Android ToggleButton Source Code Example

In this post you can learn on how to work with ToggleButton in android. Toggle button means giving two different functionalities in a single button and examples like turn loudspeaker on / off. The next example is mute/unmute the current call, etc... These are some of the examples of ToggleButton in android. And also you can change the image of the ToggleButton too in android.

Layout.xml File

<ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButtonExample" >
android:textOn="On"
android:textOff="Off"
</ToggleButton>

On activity java file under onCreate make use of the below function to work with ToggleButton.

tb = (ToggleButton) findViewById(R.id.toggleButton1);
        tb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 if (tb.isChecked()) {

                        //corresponding code units
                    } else {

                        //corresponding code units
                    }
            }
        });

No comments:

Post a Comment

My Profile