Android ToggleButton Control

Android ToggleButton Control

Android ToggleButton Control is used to display an on/off button as shown below,

Android ToggleButton Control

Usage

Use the Android ToggleButton Control for a flashlight app, or to switch on and off light.

Example

MainActivity.java

The src\main\java\com\studyopedia\studyopediaapplication\MainActivity.java is a java class which sets the views. Here we will set the View to be displayed with the setContentView.

On button click, a toast will appear with a status message showing whether Toggle Button 1 and Toggle Button 2 are On and Off. “Studyopedia is a free learning website!”. Toast in Android is a notification message visible in the form of popup.

We’re using append method in Toast to merge the result of both the Toggle Buttons,

activity_main.xml

For src\main\res\layout\activity_main.xml,

We’ll add our UI content here, 1 Button and 2 ToggleButtons. On button click, the status of the toggle buttons are shown,

AndroidManifest.xml

The content of \src\main\AndroidManifest.xml, provides key information of your app to the system. The system needs this to run your code,

strings.xml

Here’s the code for res/values/strings.xml. The app name and any hardcoded strings will be visible. We hardcoded string “Studyopedia ToggleButton Application” and used @string/title in activity_main.xml. Here, it gets mentioned under <resources>,

Output

We created an AVD, so now we will run our app using the same AVD. Go to Run > Run app or press the run button or Shift + F10.

The emulator opens up and click the first toggle button. Here’s the output, which prints the Toast message,

Android ToggleButton Output1

Now, click the second toggle button and here’s the output, which prints the Toast message. So, both the toggle buttons are Off now,

Android ToggleButton Output2

Keep both the toggle buttons in the same On state and click the RESULT button and here’s the output,

Android ToggleButton Output3

Android ImageButton Control
Android RatingBar Control
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create free tutorials for all.

No Comments

Post A Comment