Pages Navigation Menu

Coding is much easier than you think

How To Create Toggle Button in an Android Application using Eclipse:

How To Create Toggle Button in an Android Application using Eclipse:

In this tutorial, you will be Learning, How to display Toggle Button in an activity?

Here you go!!!!


Before creating an android application using Eclipse, let us see some of the key terms involved while developing this application.

Key terms involved in this project:

Intent Filters: If Intents is sent to the Android system, it will determine suitable applications for this Intent. If several components have been registered for this type of Intents, android offers the user the choice to open one of them. This determination is based on IntentFilters. An IntentFilter specifies the types of Intent that an Activity, Service, or Broadcast Receiver can respond to. An Intent Filter declares the capabilities of a component. It specifies what an Activity or Service do and what types of broadcasts a Receiver can can handle. It allows the corresponding component to receive Intents of the declared type. IntentFilters are typically defined via the AndroidManifest.xml file.

Toggle: A toggle button allows the users to change the setting between two states. You can add a basic toggle button to your layout with the ToggleButton object. When the user selects a ToggleButton, the object receives an on-click event.
** UPDATE: Android Complete tutorial now available here.
The tutorial is about how to create two toggle buttons and a normal button and when the user clicks on the normal button, it will display the current state of both toggle buttons.

Context: Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

This project is developed in Eclipse 4.2 (Juno) and tested with Android 2.2

If you are new to new to android application project, to create a new project in Eclipse refer Creation of Android Project.


Now let’s go to the coding section. This project requires following files.

Source Code:

  1. Activity (main screen)

Activity Files:

  1. activity_toggle.xml – main screen/ layout

res – Values:

  1. strings.xml – strings available in layout/activity

Manifest file:

  1. AndroidManifest.xml

Here are the coding for the above files.

package com.example.toggle; // Package Toggle
import; // Required to create an activity
import android.os.Bundle; // A mapping from String values to various Parcelable types.
import android.view.View; // For various views/ widgets
import android.view.View.OnClickListener; 
import android.widget.Toast; // Required for pop up notification message.
import android.widget.ToggleButton; // Required for using toggle button properties in activity
public class Toggle extends Activity{  // all classes extends activity
ToggleButton tb; // object for toggle class
public void onCreate(Bundle savedInstanceState) //Creates a new activity
        setContentView(R.layout.activity_toggle); // Display toggle activity when app starts (Main Screen)
        tb = (ToggleButton) findViewById(; // Obtain toggle button attributes from layout
        tb.setOnClickListener(new OnClickListener() // On clicking toggle button change the status
        			public void onClick(View v)
        				{ //display toggle button status as pop up notification
        					"Button is "+tb.getText().toString(),



Menu : activity_toggle.xml

Note: Since there are no menu settings involved in this project, delete the contents of the menu settings. This may cause errors in while building the project.







    Toggle success

Styles. xml:



Run the android application:

Android applications can be run on the android devices. You can either connect a hardware to the system to test the application or you can use the android virtual devices (AVD) Manager to create/manage the virtual devices running on emulator to run the application.

If you are new to create a Android Virtual Device (AVD), refer Creating Android Virtual Device.

To run the application/ project, Build your project using Project -> Build Project.

This will show errors in Console window at the bottom of the working area in case your project contains.

If your build is successful, Run your application using Run -> Run or Press Ctrl +F11.

Run Project

Upon running the application, the emulator will be launched which displays the AVD on your screen.

Start Emulator

To test your application, unlock the screen and double click on your app.

You can see your app with the image set during the android project creation in AVD.

AVD home screen_5

Now the application runs and the activity toggle appears as shown below. Default status is set as OFF.

Toggle button

Click on Toggle button. Now status changes to ON as shown.

Toggle button_ON

Again clicking on Toggle button changes status to OFF.

Toggle button_OFF

Thus the android application project is developed successfully.


Click Here To Share This..!!


Leave a Comment

Your email address will not be published. Required fields are marked *