Angular Material Button Toggle
Last Updated :
07 Aug, 2024
Angular Material is a UI component library that is developed by Google so that Angular developers can develop modern applications in a structured and responsive way. By making use of this library, we can greatly increase the user experience of an end-user thereby gaining popularity for our application. This library contains modern ready-to-use elements that can be directly used with minimum or no extra code.
The Button Toggle Component in the Angular material allows the user to change between on and off states for performing specific actions.
The <mat-button-toggle> is a directive in angular material. To create a toggle button or on/off button with angular material design and animations, the Angular <mat-button-toggle> directive is used. These buttons can be configured to behave like either radio buttons or checkboxes so that a single selection or multiple selections can be made on the buttons.
Syntax:
<mat-button-toggle>Toggle Button</mat-button-toggle>
The <mat-button-toggle-group> is an Angular directive used to group <mat-button-toggle> items for performing certain actions.
Syntax:
<mat-button-toggle-group #nameOfToggleGroup="matButtonToggleGroup">
<mat-button-toggle value="Toggle Button 1">
Toggle Button 1
</mat-button-toggle>
<mat-button-toggle value="Toggle Button 2">
Toggle Button 2
</mat-button-toggle>
...
</mat-button-toggle-group>
Installation Syntax:
The basic pre-requisite is that we must have Angular CLI installed on the system in order to add and configure the Angular material library. The following command is executed on the Angular CLI to install the angular material library:
ng add @angular/material
Make sure the path should be opened in the terminal before executing the above command.
Please refer to the Adding Angular Material Component to Angular Application article for the detailed installation procedure.
Adding Button Toggle Component:
To use the Button Toggle Component, we need to import it into the app.module.ts file:
import {MatButtonToggleModule} from '@angular/material/button-toggle';
To use the toggle button component in our code we have to import MatButtonToggleModule into the imports array.
Project Structure: After successful installation, the project structure will look like the following image:

Example 1: The below example illustrates the implementation of the Angular Material Button Toggle.
app.component.html
<div>
<h1>GeeksforGeeks</h1>
<h3>Angular Material Button Toggle</h3>
<mat-button-toggle-group #toggleBtn="matButtonToggleGroup">
<mat-button-toggle value="Toggle Button 1">
Toggle Button 1
</mat-button-toggle>
<mat-button-toggle value="Toggle Button 2">
Toggle Button 2
</mat-button-toggle>
</mat-button-toggle-group>
<br />
You have selected : {{ toggleBtn.value }}
</div>
app.component.ts
import { Component } from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
})
export class AppComponent {
title = "AngularApp";
}
app.module.ts
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { AppComponent } from "./app.component";
import { MatButtonToggleModule }
from "@angular/material/button-toggle";
import { BrowserAnimationsModule }
from "@angular/platform-browser/animations";
@NgModule({
imports: [
BrowserModule,
FormsModule,
MatButtonToggleModule,
BrowserAnimationsModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {}
Output:
Button toggle selection mode:
There are 2 selection modes Angular Toggle Button:
It allows only one item to be selected within the toggle group. To create a single selection toggle button,<mat-button-toggle> derivative is nested under <mat-button-toggle-group>. <mat-button-toggle-group> behaves like a radio-button group, by default, allowing only one item to be selected. When a single selection is made by the user, the value of the selected item is displayed on the UI screen.
It allows more than one item to be selected within the toggle group. To create a multiple selection toggle button,<mat-button-toggle> derivative is nested under <mat-button-toggle-group> and an attribute named as multiple, is used. Numerous items can be selected when multiple attributes are added (checkbox behavior). For multiple selections, the attribute multiple is added to the <mat-button-toggle-group> directive. When multiple selections are made by the user, the multiple values are selected and displayed, as comma-separated-values when values are viewed on the UI screen.
Example 2: The below example illustrates the implementation of the Button Toggle, by specifying the different selection modes in Angular Material.
app.component.html
<div>
<h1>GeeksforGeeks</h1>
<h3>Angular Material Button Toggle</h3>
<div>
<h4>Single selection</h4>
<mat-button-toggle-group #toggleGroup1="matButtonToggleGroup">
<mat-button-toggle value="Angular Material UI">
Angular Material UI
</mat-button-toggle>
<mat-button-toggle value="React Material UI">
React Material UI
</mat-button-toggle>
</mat-button-toggle-group>
<br />
You have selected : {{ toggleGroup1.value }}
</div>
<br />
<div>
<h4>Multiple selection</h4>
<mat-button-toggle-group #toggleGroup2="matButtonToggleGroup" multiple>
<mat-button-toggle value="Angular">
Angular
</mat-button-toggle>
<mat-button-toggle value="React">
React
</mat-button-toggle>
<mat-button-toggle value="Vue">
Vue
</mat-button-toggle>
</mat-button-toggle-group>
<br />
You have selected : {{ toggleGroup2.value }}
</div>
</div>
app.component.ts
import { Component } from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
})
export class AppComponent {
title = "AngularApp";
}
app.module.ts
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { AppComponent } from "./app.component";
import { MatButtonToggleModule }
from "@angular/material/button-toggle";
import { BrowserAnimationsModule }
from "@angular/platform-browser/animations";
@NgModule({
imports: [
BrowserModule,
FormsModule,
MatButtonToggleModule,
BrowserAnimationsModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {}
Output:
Reference: https://material.angular.io/components/button-toggle/overview
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line providing more flexibility. HTML adds Structure to a web page, CSS st
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook created React. Developers with a Javascript background can easily develop web applications
15+ min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. It is essential for both front-end and back-end developers to have a strong command of
15+ min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read