Build a Digital Alarm Clock Android App in Android Studio Using Java Full Project For Beginners

 

Welcome folks today in this blog post we will be building a digital alarm clock android app in android studio using java.All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

java 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;
}
}

 

 

 

manifest file

 

 

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
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 code

 

See also  How to Integrate Emojis Keyboard in Android App

 

<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” />

Leave a Reply