How To Build An Alarm Clock App For Android - Hive Programmers

in StemSocial10 days ago

Greetings to my favorite science community online, StemSocial.

We're back again to continue our very fun programming series. The purpose of this series is to help improve the skills and experience of beginner programmers by building basic Android apps.

So far we have successfully built a calculator app, touch light app and even a basic web browser.

For today's blog, I'm going to take you through a tutorial on building an Android Alarm clock app which you can run on your android device right at the end of this blog.

Polish_20231118_114756265.pngOriginal Image Source by Boskampi from Pixabay


Trust me guys, you'll be proud of that work and infact you can use it tonight to wake up the next morning.

I took the liberty of providing some code snippets that would help you each step of the way. If you're having trouble running the code or understanding a concept, share your comment below and I'll try to be of assistance to you.

Without wasting much time, let's get into the steps and codes shall we.


Of course as always, before starting this tutorial, you would need to ensure that the following two things are installed and running on your computer.

  • Android Studio
  • Java Development Kit (JDK)

If you're having issues with installing any of them or you want the link to downloading the softwares, please share your request in the comments section below.


Step 1: Creating a New Android Project

The first thing to do is to create a new Android project by opening Android Studio. Click on create a new project, and define the project name, package name, and select Java as the programming language.

The reason is that you could also use Kotlin which is what a lot of Android programmers are using but I wanted to use Java to teach the very basic concepts.

As we made progress in our series, I'll start using Kotlin which would create less bugs in our application.

Step 2: Designing the App User Interface

Now guys we want to design the look of our Alarm Clock app. It would be a simple and basic one but like I always promise, after the entire series, we'll start creating even more complex applications with modern User Interfaces and features.

To design the UI, go to the res/layout directory and open the activity_main.xml file.

We'll be creating a user-friendly layout including elements like TimePicker and a Button which will be for the alarm setting.

Here's how your code should look like in the activity_main.xmlpage. If it's difficult to write the code, you can just copy the one below

(html comment removed:  activity_main.xml )
<RelativeLayout xmlns:android=""


        android:text="Set Alarm"/>


Step 3: Handling Button Clicks and Setting Alarms

Now we're left with two main codes to write. The first part is to connect the button and timepicker created in activity_main.xml to the which will make clicking the buttons and setting the alarm work.

The second part is to create the logic of the alarm feature.

To do this, open page and initialize the TimePicker and Button in the onCreate method.

For the click function to work, we would need to attach a click listener to the button to initiate the alarm-setting process.

This is not as difficult as you may think guys, it's similar to the tutorial we did where we created a button which brings out a toast message when the user clicks it.

Here's a code example of how it should look like.

public class MainActivity extends AppCompatActivity {

    TimePicker timePicker;
    Button setAlarmButton;

    protected void onCreate(Bundle savedInstanceState) {

        timePicker = findViewById(;
        setAlarmButton = findViewById(;

        setAlarmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                int hour = timePicker.getCurrentHour();
                int minute = timePicker.getCurrentMinute();

                // Implement your alarm-setting logic here
                // Utilize AlarmManager to schedule the alarm

Step 4: Implementing Alarm Logic

In order for the alarm feature to work in our app we would need to use one of the built-in classes in Android studio called the AlarmManager class.

The class will be used to schedule an alarm at the specified time.

Put the following code below within the onClick method

Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);

long timeInMillis = calendar.getTimeInMillis();

alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);

Step 5: Creating our AlarmReceiver Class

Alright guys it's time for our final codes. We would need to create a receiver class for the alarm which would be responsible for implementing the action of a music playing or something happening when the alarm is triggered.

To do this, create a new Java class and name it AlarmReceiver. That class will extend BroadcastReceiver class. In summary, this class will handle the alarm when it triggers.

Here's how your code should look like.

public class AlarmReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        // Implement actions for when the alarm activates
        // This may involve displaying a notification, playing a sound, etc.
        Toast.makeText(context, "Alarm! Wake up!", Toast.LENGTH_SHORT).show();

Running The App

That's it for the entire code guys. We're ready to run our Alarm clock app now. Click the green play button at the top of the Android Studio task bar and the app will run.

You can either connect your android device and ensure that USB debugging is turned on for it to run.

You could also use the built-in Android emulator in Android studio.


Thanks for taking the time to read today's tutorial blog. I hope you enjoyed it guys. As I always say, practicing and building more different apps will improve your android programming skills.

If you have any issues writing the codes or setting up your Android Studio IDE, please let me know in the comments section below.

Have a lovely day and catch you next time on StemSocial. Goodbye 👨‍💻


Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support.