How To Build an Alarm Clock Android App - Hive Programmers

avatar

Greetings to my favorite science community online, StemSocial.

It's @skyehi and in today's episode following our over 1 month old series on Android App Development Tutorial for beginners, we're going to be building another basic App as I promised everyone yesterday.

In today's tutorial, I'll take you through the steps in building an awesome and fully functional Alarm Clock Android App.

Polish_20231217_195228132.jpg
Original Image Source by Pixabay from Pexels

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Alarm Clock Apps are very essential tools in our daily lives. Most people use it to avoid waking up late for work or an engagement. Some use it as a way to time themselves to complete a task.

I believe Alarm Clocks are really useful and as a developer, learning how to build one will improve your skills and usefulness.

Alright guys without wasting anymore time, I'll take you through the steps. I'm pretty sure you if you read yesterday's successful blog, you probably know the basic steps to completing our App.

Prerequisites

As always I'll keep bringing this step for the benefit of newcomers to my series. In order to be able to go through with the series and successfully build your own Android Apps, you need two main things;

  • Android Studio IDE installed.
  • Java Development Kit, JDK installed

If you're having trouble installing any of these softwares please let me know in the comments guys.

After all the required softwares are ready we'll start by creating a new Android Studio Project.

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Creating a New Android Studio Project

Alright guys, first open your Android Studio IDE and click on "Create a new Android project". As usual guys we'll be choosing an Empty Activity template as the template of our APP.

At this point you can set your app project name, package name, and also guys don't forget to choose Java language as the programming language because that's the language we'll be using for the basic tutorials.

Like I promised in previous blogs, as we make progress and get to intermediary level, we'll switch it up and use Kotlin instead. Kotlin will help us create simpler codes with less bugs in the App.

When you're satisfied with the project setup, click finish and wait for Android Studio to build your App Project for you.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Designing our App User Interface

It's time to work on the frontend or design part of our App. As I thought yesterday in my blog, the frontend is the part of the App the user sees and interacts with.

Well guys, this is still a beginner's guide and we're still writing codes for basic Apps so I'll keep things nice and easy for everyone.

In our Alarm clock App design layout we'll have three main elements; a timepicker to select the alarm time, a button to set the alarm and a textview that would show our App users the status of the alarm they set. Pretty simple and straightforward right guys.

The App's frontend design layout code will be written inside our activity_main.xml file.

Here's how the code should look like.

(html comment removed:  activity_main.xml )
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TimePicker
        android:id="@+id/timePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/setAlarmButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set Alarm"
        android:layout_below="@id/timePicker"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"/>

    <TextView
        android:id="@+id/alarmStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_below="@id/setAlarmButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"/>
</RelativeLayout>

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Implementing Alarm Logic

Since we're through with our front end design code, it's now time for the backend code. This is where we'll be writing the code to handle the logic that sets alarms.

I'm super excited with this particular tutorial because it would give me a chance to introduce another default class and API in Android Studio, the AlarmManager class.

Our work will be pretty easy because Android Studio already has a default class for setting alarms.

Here's how your logic code should look like guys

In the MainActivity.java file, handle the logic for setting alarms using the AlarmManager class.

// MainActivity.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private TimePicker timePicker;
    private Button setAlarmButton;
    private TextView alarmStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        timePicker = findViewById(R.id.timePicker);
        setAlarmButton = findViewById(R.id.setAlarmButton);
        alarmStatus = findViewById(R.id.alarmStatus);

        setAlarmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setAlarm();
            }
        });
    }

    private void setAlarm() {
        int hour = timePicker.getCurrentHour();
        int minute = timePicker.getCurrentMinute();

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent alarmIntent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);

        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

        alarmStatus.setText("Alarm set for " + hour + ":" + minute);
    }
}

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Creating the Alarm Receiver

Well guys the backend code is not yet complete. I'll also be introducing you to a whole new element in Android Studio; Broadcast Receivers.

I'll definitely be making an entirely separate blog on this particular element soon but for now guys all we need to do is to create a new Java class and name it AlarmReceiver.java to be able to create our BroadcastReceiver.

What this receiver would do is to handle the Alarm event.

Receivers are necessary for sending notifications when certain events happen. Without this step, the alarm feature will not work on your phone.

It's a pretty simple code guys, nothing too complicated. We can do stuff like showing notification when Alarm goes off or even make a simple toast.

Here's how your code should look like guys

// AlarmReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Code to handle alarm event, e.g., show a notification
    }
}

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Requesting Set Alarm Permissions

Now guys for the last part of our App project before running the App, we're going to request a permission.

Since our App is an alarm clock App, it would definitely need to access the alarming feature of our Android device.

As usual, permission request code will be written inside our AndroidManifest.xml file, include the necessary permissions.

Here's how your permission request code should look like

(html comment removed:  AndroidManifest.xml )
<uses-permission android:name="android.permission.SET_ALARM" />

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Running Our App

Alright guys, we're all set with our App. Congratulations on building your very first Alarm Clock App. It's time to launch the App and test the alarm functionality.

You can run the App on an emulator or your physical Android device. When your App launches, you can set an alarm using the TimePicker and check that your alarm triggers at the specified time.

Thanks so much for taking the time to read today's blog. I hope you enjoyed this tutorial guys.

As always if you're having troubles writing the code, running the App or even installing the necessary development tools, please let me know in the comments. Have fun out there guys.

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


You Can Follow Me @skyehi For More Like This And Others



0
0
0.000
2 comments
avatar

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. 
 

0
0
0.000
avatar

Congratulations @skyehi! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You have been a buzzy bee and published a post every day of the week.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

LEO Power Up Day - December 15, 2023
0
0
0.000