Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Build a Simple Notes App in Android?
Next article icon

How to Build a Simple Alarm Setter App in Android?

Last Updated : 09 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

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: 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:

AndroidManifest.xml

XML
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools">      <uses-permission android:name="android.permission.VIBRATE" />     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />      <application         android:allowBackup="true"         android:dataExtractionRules="@xml/data_extraction_rules"         android:fullBackupContent="@xml/backup_rules"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:roundIcon="@mipmap/ic_launcher_round"         android:supportsRtl="true"         android:theme="@style/Theme.Application_Java"         tools:targetApi="31">         <activity             android:name=".MainActivity"             android:exported="true">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>           </activity>          <receiver             android:name=".AlarmReceiver"             android:enabled="true"             android:exported="false" />     </application>  </manifest> 


Step 3: 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.

activity_main.xml

XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <!--Added Time picker just to pick the alarm time-->     <!--gravity is aligned to center-->     <TimePicker         android:id="@+id/timePicker"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center" />      <!--Added Toggle Button to set the alarm on or off-->     <!--ByDefault toggleButton is set to false-->     <ToggleButton         android:id="@+id/toggleButton"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_margin="20dp"         android:checked="false"         android:onClick="OnToggleClicked" />      <!--"OnToggleClicked" method will be implemented in MainActivity.java -->      </LinearLayout> 


Step 4: 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.

MainActivity.java

Java
import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TimePicker; import android.widget.Toast; import android.widget.ToggleButton;  import androidx.appcompat.app.AppCompatActivity;  import java.util.Calendar;  public class MainActivity extends AppCompatActivity {     TimePicker alarmTimePicker;     PendingIntent pendingIntent;     AlarmManager alarmManager;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);         alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);      }      // OnToggleClicked() method is implemented the time functionality     public void OnToggleClicked(View view) {         long time;         if (((ToggleButton) view).isChecked()) {             Toast.makeText(MainActivity.this, "ALARM ON", Toast.LENGTH_SHORT).show();             Calendar calendar = Calendar.getInstance();              // calendar is called to get current time in hour and minute             calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());             calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());              // using intent i have class AlarmReceiver class which inherits             // BroadcastReceiver             Intent intent = new Intent(this, AlarmReceiver.class);              // we call broadcast using pendingIntent             pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);              time = (calendar.getTimeInMillis() - (calendar.getTimeInMillis() % 60000));             if (System.currentTimeMillis() > time) {                 // setting time as AM and PM                 if (Calendar.AM_PM == 0)                     time = time + (1000 * 60 * 60 * 12);                 else                     time = time + (1000 * 60 * 60 * 24);             }             // Alarm rings continuously until toggle button is turned off             alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent);             // alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (time * 1000), pendingIntent);         } else {             alarmManager.cancel(pendingIntent);             Toast.makeText(MainActivity.this, "ALARM OFF", Toast.LENGTH_SHORT).show();         }     } } 


Step 5: 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.

AlarmReceiver.java

Java
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; import android.os.Vibrator; import android.widget.Toast;  import androidx.annotation.RequiresApi;  public class AlarmReceiver extends BroadcastReceiver {     @RequiresApi(api = Build.VERSION_CODES.Q)     @Override     // implement onReceive() method     public void onReceive(Context context, Intent intent) {          // we will use vibrator first         Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);         vibrator.vibrate(4000);          Toast.makeText(context, "Alarm! Wake up! Wake up!", Toast.LENGTH_LONG).show();         Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);         if (alarmUri == null) {             alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);         }                  // setting default ringtone         Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);          // play ringtone         ringtone.play();     } } 


Step 6: 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”. 

XML
<?xml version="1.0" encoding="utf-8"?> <resources>     <color name="colorPrimary">#0F9D58</color>     <color name="colorPrimaryDark">#0F4C2E</color>     <color name="colorAccent">#9D0F9B</color> </resources> 


Step 7: 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. 

XML
<resources>        <!-- Base application theme. -->     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">         <!-- Customize your theme here. -->         <item name="colorPrimary">@color/colorPrimary</item>         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>         <item name="colorAccent">@color/colorAccent</item>     </style>  </resources> 


Output:




Next Article
How to Build a Simple Notes App in Android?

A

annulata2402
Improve
Article Tags :
  • Android
  • Java
  • Project
  • Android Projects
  • Java-Android
Practice Tags :
  • Java

Similar Reads

  • How to Build a Simple Notes App in Android?
    Notes app is used for making short text notes, updating when you need them, and trashing when you are done. It can be used for various functions as you can add your to-do list in this app, some important notes for future reference, etc. The app is very useful in some cases like when you want quick a
    9 min read
  • How to Build a Sensor App in Android?
    Android mobile phones have sensors, so we can perform various functions like Controlling screen brightness, Monitoring acceleration along a single axis, Motion detection, etc. In this article, we will be building an application that will determine the intensity of light in the room with the help of
    5 min read
  • How to Build a Simple Magic 8 Ball App in Android?
    In this article, we will be building a Magic 8 Ball App Project using Java and XML in Android. The application is based on a decision making application. In this app, a user can ask the ball what decision they should make. The ball will randomly answer Yes, No, Not Sure, and other such answers. Ther
    3 min read
  • How to Build a Simple Reflex Game in Android?
    A reflex game is a simple fun game that measures your responding speed. It is quite simple to make and understand. We will be designing a reflex game that will calculate your responding speed. The rules are simple just press the stop button when you see the change in background color, and the time y
    4 min read
  • How to Build a Rick and Morty App in Android?
    Rick and Morty is an American animated science fiction sitcom created by Justin Roiland and Dan Harmon. In this article, we will build an application that will display the name and image of all rick and Morty characters using this API. In order to build this application we will be using the Retrofit
    5 min read
  • How to Build a Simple Music Player App using Android Kotlin
    This is a very simple app suitable for beginners to learn the concepts. The following things you will learn in this article: Implementing MediaPlayer class and using its methods like pause, play and stop. Using external files ( images, audio, etc ) in our project.Building the interface of our Music
    6 min read
  • How to Build a Simple Torch App in Android using Kotlin?
    Torch Application is a very basic application that every beginner level android developer should definitely try to build while learning Android. In this article, we will be creating an application in which we will simply display a toggle button to switch on and switch off the torch. Note: If you are
    4 min read
  • How to Build a Weather App in Android?
    In this project, we will be building a weather application. This application will show the temperature of a location. To fetch weather information we will need an API. An API(Application Programming Interface) is a function that allows applications to interact and share data using various components
    6 min read
  • How to Build a Pomodoro App in Android?
    The Pomodoro Technique is a time management method developed by Francesco Cirillo in the late 1980s. The technique uses a timer to break down work into intervals, traditionally 25 minutes in length, separated by short breaks of 5 minutes. These intervals are known as "pomodoros". The method is based
    4 min read
  • How to Build a Simple Music Player App Using Android Studio
    This is a very simple app suitable for beginners to learn the concepts. The following things you will learn in this article: Implementing MediaPlayer class and using its methods like pause, play and stop. Using external files ( images, audio, etc ) in our project.Building the interface of our Music
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences