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

avatar

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

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

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.

Prerequisites

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.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

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="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:layout_below="@id/timePicker"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:text="Set Alarm"/>
</RelativeLayout>

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

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 MainActivity.java 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 MainActivity.java 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.

// MainActivity.java
public class MainActivity extends AppCompatActivity {

    TimePicker timePicker;
    Button setAlarmButton;

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

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

        setAlarmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            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

// MainActivity.java
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.

// AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {

    @Override
    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.

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

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 👨‍💻



0
0
0.000
1 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