In Android, ToggleButton is just like a switch containing two states either ON or OFF which is represented using boolean values true and false respectively. ToggleButton unlike switch does not have a slider interface i.e we cannot slide to change the states. It is just like a button. In this article, we will be discussing how to create a ToggleButton in Kotlin dynamically.
Note: ToggleButton inherits the button class of android. Therefore, all the attributes of the button are also applicable here.
Following are some of the additional important attributes available along ToggleButton
|android:disabledAlpha||It is used to adjust the alpha value of the button when it is disabled|
|android:textOn||The text which is shown when button is on or checked|
|android:textOff||The text which is shown when button is off or unchecked|
Create a new project in Android Studio
To create a new project in Android Studuio follow these steps:
- Click on File, then New and then New Project and give name whatever you like.
- Choose “Empty Activity” for the project template.
- Then, select Kotlin language Support and click next button.
- Select minimum SDK, whatever you need
This is how your project directory should look like:
Modify activity_main.xml file
Create ToogleButton in MainActivity.kt file
Insert following code in your
Run on Emulator
- ToggleButton in Kotlin
- Dynamic CheckedTextView in Kotlin
- Dynamic RadioButton in Kotlin
- Dynamic Switch in Kotlin
- Dynamic SeekBar in Kotlin
- Dynamic RatingBar in Kotlin
- Dynamic ImageButton in Kotlin
- Dynamic AutoCompleteTextView in Kotlin
- Dynamic DatePicker in Kotlin
- Dynamic TimePicker in Kotlin
- Dynamic TextClock in Kotlin
- Dynamic Spinner in Kotlin
- Dynamic Chronometer in Kotlin
- Dynamic ProgressBar in Kotlin
- Dynamic ImageSwitcher in Kotlin
- Dynamic TextSwitcher in Kotlin
- Dynamic ImageView in Kotlin
- Dynamic TextView in Kotlin
- Dynamic Button in Kotlin
- Dynamic ScrollView in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.