Build a Digital Alarm Clock Android App in Android Studio Using Java

Everyone has every option I have Build a Digital Alarm Clock Android App in Android Studio Using Java I have the option to write this article. I am writing this article to resolve your doubts. We ask that you understand this article well.

Build a Digital Alarm Clock Android App in Android Studio Using Java

Build a Digital Alarm Clock Android App in Android Studio Using Java

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

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

Read Also: Build a Rich Text Editor Android App in Android Studio

Final Words

We hope the Build a Digital Alarm Clock Android App in Android Studio Using Java article fills your doubts. And let us know if you have any doubts. We resolve your doubts Thank you.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment