Using Toggle Button

A toggle button allows the user to change a setting between two states.

For adding a ToggleButton to your layout file (res/layout/*.xml)

        android:text="ToggleButton" />

For adding the functionality part of the ToggleButton, copy and paste the below code in your onCreate() function of JAVA File.

Note: Below the SetContentView() line in onCreate() function.

		ToggleButton tb = (ToggleButton) findViewById(;
		tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					Toast.makeText(MainActivity.this, "true",
				} else {
					Toast.makeText(MainActivity.this, "false",

Save the changes and run your Android Application in Emulator or an Android Device. Post your comments.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s