In this article, we are going to see how to build a much interesting app named Alarm Setter. Alarm plays a vital role in our day-to-day life. Nowadays alarm has become our wake-up assistant. Every mobile phone is associated with an alarm app. We will create this app using android studio. Android Studio provides a great unified environment to build apps for Android phones, tablets, Android Wear, Android TV, and Android Auto because it provides a very large number of app building features and it is also very easy to use. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 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: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. In this file, we have added two items ‘TimePicker’ and ‘ToggleButton’. TimePicker is used to capture the alarm time and ToggleButton is added to set the alarm on or off. Initially, ToggleButton is set to off. It is set on when an alarm is set. Below is the code for the activity_main.xml file.
Step 3: Working with the MainActivity.java file
Go to MainActivity.java Class. In MainActivity.java class onToggleClicked( ) method is implemented in which the current hour and the minute is set using the calendar. Alarm services are implemented using AlarmManager class. The alarm is set in such a way that it rings and vibrates repeatedly until the toggle button is turned off. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Step 4: Working with BroadCastReceiver (AlarmReceiver) class
Create a new java class named “AlarmReceiver.java” at the same place where MainActivity.java class resides. In this class onReceive() method is implemented. Here we have added vibration functionality and a default ringtone that starts to vibrate and ring when the alarm time is scheduled. Below is the code for the AlarmReceiver.java file. Comments are added inside the code to understand the code in more detail.
Step 5: Playing with colors
Go to the “values” folder first then choose the colors.xml file. In the colors.xml file, you can keep colors of your choice as many as you want to use in your app. You have to just give the name and put the color code of the respective colors. I have kept the AppBar color as “#0F9D58” which we have named as “colorPrimary”.
Step 6: Changing theme of the app
Go to the “values” folder first then choose the themes.xml file. In the theme.xml file, we have used “Theme.AppCompat.Light.DarkActionBar” which is a light theme with a dark ActionBar. We can use a light theme with a light action bar using “Theme.AppCompat.Light.LightActionBar”, it all depends on our choice and need.
Step 7: Adding permission in “AndroidManifest.xml”
Go to the “AndroidManifest.xml” file. A BroadcastReceiver is registered in AndroidManifest.xml by adding a receiver section after the application section is over. Also, give permission to vibrate using:
<uses-permission android:name=”android.permission.VIBRATE” />
Here we have manually set the alarm time. You can also set it by adjusting the clock showing in front. You will have to wait for the alarm time. It will continuously show “Alarm! Wake up! Wake up!” and rings and vibrates until the toggle-button is turned off. You can get Source code on the given GitHub link below: https://github.com/Babitababy/Alarm_setter
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.