Introduction
Android is one of the most popular operating systems for mobiles. The alarm app takes place in day by day Android usage. Each Android mobile contains an alarm application. Simply alarm is our wake-up assistant. So, I will show you how to create an alarm android application using Android Studio. Android is the kernel-based operating system.it allows the user can modify the GUI components and source code.
Requirements
- Android Studio
- Little bit XML and JAVA knowledge.
- Android Emulator (or) Android mobile
Download link (Android Studio): https://developer.android.com/studio/index.html
Steps should be followed
Carefully follow my steps to create an Alarm Android Application using Android Studio. I have included the source code below.
Step 1
Open the Android Studio and start a new project.
Step 2

Put the application name and company domain. If you wish to use C++ for coding the project, mark the "Include C++ support" checkbox and then click Next.
Step 3

Select the Android minimum SDK. After you chose the minimum SDK, it will show an approximate percentage of people who use that SDK. Click Next.
Step 4

Choose the basic activity, then click Next.
Step 5

Put the activity name and layout name. Android Studio basically takes the java class name that you provide as the activity name and click Finish.
Step 6

Go to activity_main.xml then click the text bottom. This xml file contains the designing code for the android app. Into the activity_main.xml copy and paste the below code.
Activity_main.xml code
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fitsSystemWindows="true"
- tools:context="ganeshannt.alarm.MainActivity">
- <android.support.design.widget.AppBarLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:theme="@style/AppTheme.AppBarOverlay">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:popupTheme="@style/AppTheme.PopupOverlay" />
- </android.support.design.widget.AppBarLayout>
- <include layout="@layout/content_main" />
- </android.support.design.widget.CoordinatorLayout>

Create a new content_main.xml file (File ⇒ New ⇒Activity⇒Empty_activity).
Go to content_main.xml then click the text bottom. This xml file contains the designing code for the android app. Into the content_main.xml copy and paste the below code.
content_main.xml code
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fitsSystemWindows="true"
- tools:context="ganeshannt.alarm.MainActivity">
- <android.support.design.widget.AppBarLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:theme="@style/AppTheme.AppBarOverlay">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:popupTheme="@style/AppTheme.PopupOverlay" />
- </android.support.design.widget.AppBarLayout>
- <include layout="@layout/content_main" />
- </android.support.design.widget.CoordinatorLayout>

- package ganeshannt.alarm;
- import android.annotation.TargetApi;
- import android.app.AlarmManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.TimePicker;
- import java.util.Calendar;
- public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
- //to make our alarm manager
- AlarmManager alarm_manager;
- TimePicker alarm_timepicker;
- TextView update_text;
- Context context;
- PendingIntent pending_intent;
- int choose_whale_sound;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- this.context = this;
- // initialize our alarm manager
- alarm_manager = (AlarmManager) getSystemService(ALARM_SERVICE);
- //initialize our timepicker
- alarm_timepicker = (TimePicker) findViewById(R.id.timePicker);
- //initialize our text update box
- update_text = (TextView) findViewById(R.id.update_text);
- // create an instance of a calendar
- final Calendar calendar = Calendar.getInstance();
- // create an intent to the Alarm Receiver class
- final Intent my_intent = new Intent(this.context, Alarm_Receiver.class);
- // create the spinner in the main UI
- Spinner spinner = (Spinner) findViewById(R.id.richard_spinner);
- // Create an ArrayAdapter using the string array and a default spinner layout
- ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
- R.array.whale_array, android.R.layout.simple_spinner_item);
- // Specify the layout to use when the list of choices appears
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- // Apply the adapter to the spinner
- spinner.setAdapter(adapter);
- // Set an onclick listener to the onItemSelected method
- spinner.setOnItemSelectedListener(this);
- // initialize start button
- Button alarm_on = (Button) findViewById(R.id.alarm_on);
- // create an onClick listener to start the alarm
- alarm_on.setOnClickListener(new View.OnClickListener() {
- @TargetApi(Build.VERSION_CODES.M)
- @Override
- public void onClick(View v) {
- // setting calendar instance with the hour and minute that we picked
- // on the time picker
- calendar.set(Calendar.HOUR_OF_DAY, alarm_timepicker.getHour());
- calendar.set(Calendar.MINUTE, alarm_timepicker.getMinute());
- // get the int values of the hour and minute
- int hour = alarm_timepicker.getHour();
- int minute = alarm_timepicker.getMinute();
- // convert the int values to strings
- String hour_string = String.valueOf(hour);
- String minute_string = String.valueOf(minute);
- // convert 24-hour time to 12-hour time
- if (hour > 12) {
- hour_string = String.valueOf(hour - 12);
- }
- if (minute < 10) {
- //10:7 --> 10:07
- minute_string = "0" + String.valueOf(minute);
- }
- // method that changes the update text Textbox
- set_alarm_text("Alarm set to: " + hour_string + ":" + minute_string);
- // put in extra string into my_intent
- // tells the clock that you pressed the "alarm on" button
- my_intent.putExtra("extra", "alarm on");
- // put in an extra int into my_intent
- // tells the clock that you want a certain value from the drop-down menu/spinner
- my_intent.putExtra("whale_choice", choose_whale_sound);
- Log.e("The whale id is" , String.valueOf(choose_whale_sound));
- // create a pending intent that delays the intent
- // until the specified calendar time
- pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0,
- my_intent, PendingIntent.FLAG_UPDATE_CURRENT);
- // set the alarm manager
- alarm_manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
- pending_intent);
- }
- });
- // initialize the stop button
- Button alarm_off = (Button) findViewById(R.id.alarm_off);
- // create an onClick listener to stop the alarm or undo an alarm set
- alarm_off.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // method that changes the update text Textbox
- set_alarm_text("Alarm off!");
- // cancel the alarm
- alarm_manager.cancel(pending_intent);
- // put extra string into my_intent
- // tells the clock that you pressed the "alarm off" button
- my_intent.putExtra("extra", "alarm off");
- // also put an extra int into the alarm off section
- // to prevent crashes in a Null Pointer Exception
- my_intent.putExtra("whale_choice", choose_whale_sound);
- // stop the ringtone
- sendBroadcast(my_intent);
- }
- });
- }
- private void set_alarm_text(String output) {
- update_text.setText(output);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- // An item was selected. You can retrieve the selected item using
- // parent.getItemAtPosition(pos)
- // outputting whatever id the user has selected
- //Toast.makeText(parent.getContext(), "the spinner item is "
- // + id, Toast.LENGTH_SHORT).show();
- choose_whale_sound = (int) id;
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- // Another interface callback
- }
- }
Step 9
Create a new Alarm_Receiver.java file (File ⇒ New ⇒Java class).
Into the Alarm_Receiver.java copy and paste the below code.java programming is the backend language for android. Do not replace your package name otherwise, the app will not run.
Alarm_Receiver.java code




Hari KrishnanPosted Jul 3, 2020, 12:54 PM
Does this work of the phone is locked?
Md.Rashedul IslamPosted Feb 22, 2019, 2:49 AM
I think content_main.xml code is wrong.... please... help
Md.Rashedul IslamPosted Feb 22, 2019, 2:48 AM
Please send Source code to [email protected]
Rushi MehtaPosted Oct 13, 2018, 3:02 AM
Thanks for sharing
Arvind ChourasiyaPosted Sep 8, 2017, 2:33 AM
Hi sir nice one. Here i need morning 7 AM Indian time how i can set in calendar.? and i need setRepeate() type alarm in morning every day. Thank u
Devaki ThenmozhiPosted Jun 17, 2017, 6:27 AM
Nice article .....:):):)
Ganeshan NPosted Jun 14, 2017, 12:24 PM
Thanks Mr.saravana kumar.............
Saravanakumar SekaranPosted Jun 14, 2017, 11:31 AM
Wow simply superb !!!