Create a simple Alarm Clock App in Android Studio

This video shows complete steps and demo to design and develop an Alarm clock App using Android Studio.

[Source Code]

package com.example.maalarm.myalarm;

import android.media.Ringtone;
import android.media.RingtoneManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextClock;
import android.widget.TimePicker;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

TimePicker alarmTime;
TextClock currentTime;

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

alarmTime = findViewById(R.id.timePicker);
currentTime = findViewById(R.id.textClock);
final Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));

Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {

if (currentTime.getText().toString().equals(AlarmTime())){
r.play();
}else{
r.stop();
}
}
}, 0, 1000);
}

public String AlarmTime(){

Integer alarmHours = alarmTime.getCurrentHour();
Integer alarmMinutes = alarmTime.getCurrentMinute();
String stringAlarmMinutes;

if (alarmMinutes<10){
stringAlarmMinutes = “0”;
stringAlarmMinutes = stringAlarmMinutes.concat(alarmMinutes.toString());
}else{
stringAlarmMinutes = alarmMinutes.toString();
}
String stringAlarmTime;

if(alarmHours>12){
alarmHours = alarmHours – 12;
stringAlarmTime = alarmHours.toString().concat(“:”).concat(stringAlarmMinutes).concat(” PM”);
}else{
stringAlarmTime = alarmHours.toString().concat(“:”).concat(stringAlarmMinutes).concat(” AM”);
}
return stringAlarmTime;
}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.maalarm.myalarm”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”com.example.maalarm.myalarm.MainActivity”>

<TimePicker
android:id=”@+id/timePicker”
android:layout_width=”300dp”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true” />

<TextClock
android:id=”@+id/textClock”
android:layout_width=”150dp”
android:layout_height=”80dp”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”37dp” />
</RelativeLayout>

Top posts/ comments from YouTube channel

Time picker is gone from palette. What to do?

 

Oh … yes. Wow. So, it seems like google has removed the Date Time option from the palette of Android Studio from version 3.1. It was there in 3.0. Anyway, this issue has been discussed on other forums as well.
The only workaround which I can think of is to manually add the time picker and time clock code in the XML format (text file).
I am copying the snippet below which can be used in the layout XML file:

<TimePicker
android:id=”@+id/timePicker”
android:layout_width=”300dp”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true” />

<TextClock
android:id=”@+id/textClock”
android:layout_width=”150dp”
android:layout_height=”80dp”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”37dp” />

We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

12 comments

  1. When i run the app, it produces a security exception and requires a permission, what is the permission that i can use to play the ringtone?

    • No such special permission is required to access or play ringtone. As long as ringtone is correctly created using the below Ringtone manager, it should work.

      Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));

      Let me know what is the exception is being thrown?

  2. Hello, so i ran your program on a virtual phone just like in the video and it worked fine, but when i connect as a test device my real phone the app has a problem in the result phase. It does not ring when it’s time so it’s like it doesn’t work at all. Why does this happen? As i said, in the virtual device it works perfectly. Is there a permission problem with a real phone’s ringtone? I have a Samsung Galaxy S7 Edge. Thanks a lot, the video is very helpful!

    • I don’t think there is any special permission needed to access ringtone through ringtone manager as shown in the tutorial. Though there might be some exceptions being thrown while running the App in your Samsung Galaxy S7. Unfortunately, I don’t have this model of phone otherwise I would have tried myself.

      What I can suggest is to go to the developer options in your phone and then debug the code in Android Studio directly while your App runs on your Phone. This will be helpful in finding the exception it is throwing.

      • Hello again, so as i realized the problem was with the 24hour settings and the language of the device. Sadly this simple version you created works only for english-12hour settings but i think i can find a way to resolve this issue. Again thanks a lot for all the help!!!

Leave a Reply