{"id":4218,"date":"2020-06-25T22:49:19","date_gmt":"2020-06-25T12:49:19","guid":{"rendered":"https:\/\/learntodroid.com\/?p=4218"},"modified":"2020-08-14T16:17:40","modified_gmt":"2020-08-14T06:17:40","slug":"how-to-create-a-button-group-with-materialbuttontogglegroup","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/how-to-create-a-button-group-with-materialbuttontogglegroup\/","title":{"rendered":"How to Create a Button Group with MaterialButtonToggleGroup"},"content":{"rendered":"\n

The material design library for Android offers two components MaterialButtonToggleGroup and MaterialButton that can be used to create a group of buttons can toggled on or off while having only a single button being selected at any time.<\/p>\n\n\n\n

See an example of the MaterialButtonToggleGroup and MaterialButton widgets used below to select the period of data for stock prices to display in a chart in an Android app.<\/p>\n\n\n\n

\"\"
Example usage of the MaterialButtonToggleGroup in Android<\/figcaption><\/figure><\/div>\n\n\n\n

In this post I will show you, with code samples written in Java (also available in GitHub<\/a>), how to use the MaterialButtonToggleGroup and MaterialButton widgets from the material design library for Android to create a group of buttons that can be toggled on and off. <\/p>\n\n\n\n

How to use the MaterialButtonToggleGroup and MaterialButton Widgets in Android<\/h2>\n\n\n\n

In this section of the tutorial on using the MaterialButtonToggleGroup and MaterialButton widgets I will show you code samples on how to create a set of buttons that can be toggled.<\/p>\n\n\n\n

Step 1: Retrieve the Android Material Design Dependencies using Gradle<\/h3>\n\n\n\n

First we will need to add the implementation dependency for the Android material design library<\/a> to our Android project in order to use the MaterialButtonToggleGroup and MaterialButton widgets.<\/p>\n\n\n\n

In your build.gradle file inside in the app directory in your Android project, add the following dependency to get access to the MaterialButtonToggleGroup and MaterialButton widgets.<\/p>\n\n\n