![]() |
In Android, Switch is a two-state toggle switch widget that is used to select a choice between two options. It is generally an on/off button that indicates the current state of the switch. Normal Basic Features for which switch can be used in ActionBar of application:
Now let us see the step-by-step implementation to do so. Step by Step ImplementationStep 1: Create a New Project To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language. Step 2: Go to “res” and create a new directory and name it as “menu” Refer to this Create Menu Folder & Menu File in Android Studio article and create a new Menu resource file and name it as “action_menu“. Also, go to layout and create a new layout resource file and name it as “use_switch“. Step 3: Open the action_menu.xml file and use the following code in it XML
Step 4: Open the use_switch.xml file and use the following code in it XML
Step 5: Working with the activity_main.xml file Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. XML
Step 6: Working with the MainActivity.java file Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Java
Output: Hence we developed an application where we can use the switch in the action bar of the application to implement basic features of an app. The same process can be applied in any other application to add a switch in ActionBar and can be used to implement any feature according to the need of the application. |
Reffered: https://www.geeksforgeeks.org
Android |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 10 |