How to make Simple Weather Application in Android Studio

How to make Simple Alarm Application in Android Studio (Source Code)


Introduction

In this blog, we will learn to create a simple alarm application in an android studio. This is a simple application for beginner.

Alarm application

We will build a simple alarm application that will start the alarm after some fixed amount of time and the user will be able to stop the alarm by pressing the stop button.

Following are some steps we need in order to make this application.
  1. Take time from a user
  2. Set countdown
  3. Play/Start alarm

1) Take time from a user

First, we will take time from a user through TextView. In this example, we will take time in seconds. 

2) Set countdown and 3) Play Alarm

After that, we will start a countdown for that amount of time. CountDownTimer will automatically start playing alarm when countdown finish with the help of mediaPlayer

try{
      int time = Integer.parseInt(plainText.getText().toString());

      //if you want to start application from fixed time then add to 90,100 milli-second
      final int milliSecond = (time * 1000) + 100;
      //value is needed in milli-second. so first we convert value/time into milli-second
      new CountDownTimer(milliSecond,1000){//1000 is equal to 1 second

      @Override
      public void onTick(long millisUntilFinished) {
          textView.setText("00.0" +String.valueOf(millisUntilFinished/1000));
      }

      @Override
      public void onFinish() {
           textView.setText("Alarm");
           mediaPlayer.start();
      }
     }.start();
    }catch (NumberFormatException e){
           Toast.makeText(this,"Enter Value in integer Only",Toast.LENGTH_LONG).show();
   }

That's it. Following is the complete source code of application.

MainActivity.java

package com.example.alaramapp;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    TextView plainText;
    TextView textView;
    MediaPlayer mediaPlayer;

    public void select(View view){
        plainText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);

        mediaPlayer = MediaPlayer.create(this,R.raw.alarm);
        try{
            int time = Integer.parseInt(plainText.getText().toString());

            //if you want to start application from fixed time then add to 90,100 milli-second
            final int milliSecond = (time * 1000) + 100;
                    //value is needed in milli-second. so first we convert value/time into milli-second
                    new CountDownTimer(milliSecond,1000){//1000 is equal to 1 second

                        @Override
                        public void onTick(long millisUntilFinished) {
                            textView.setText("00.0" +String.valueOf(millisUntilFinished/1000));
                        }

                        @Override
                        public void onFinish() {
                            textView.setText("Alarm");
                            mediaPlayer.start();
                        }
                    }.start();
        }catch (NumberFormatException e){
            Toast.makeText(this,"Enter Value in integer Only",Toast.LENGTH_LONG).show();
        }

    }

    public void stop(View view){
        mediaPlayer.stop();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="132dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:ems="10"
        android:hint="Enter Time In Seconds"
        android:inputType="textPersonName"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.228"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:background="#FF16203D"
        android:padding="8dp"
        android:text="00.00"
        android:textColor="#FFFFFFFF"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText"
        app:layout_constraintVertical_bias="0.31" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:layout_marginBottom="8dp"
        android:onClick="select"
        android:text="select"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.223" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="stop"
        android:text="Stop alarm"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="68dp"
        android:background="#FF585D6B"
        android:paddingLeft="23dp"
        android:paddingTop="15dp"
        android:text="ALARM APPLICATION"
        android:textSize="30sp"
        android:textStyle="bold"
        app:fontFamily="monospace"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>


Comments

  1. Baccarat - The Ultimate Guide to Baccarat in America - WURRI
    Baccarat is a variation of the popular game of strategy. It uses only four cards: one point or more. When you are ready to 실시간 바카라 사이트 추천 play, a hand of

    ReplyDelete

Post a Comment