Open In App

Build an Android App to Check a Year is Leap Year or Not

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to make an App to check a year is a leap year or not. Before that, we need to have an idea of what is a leap year? If a year is:

  1. Multiple of 400
  2. multiple of 4 and not multiple of 100

Then the year is a leap year.

Algorithm

  • if the year is divisible by 400 then it is a leap year.
  • else if the year is divisible by 100 then it is not a leap year.
  • else if the year is divisible by 4 then it is a leap year.
  • else it is not a leap year.

Now we will make an App for that. So, if we find a year is leap or not we will display a Toast.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@color/teal_200">
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Leap year or Not"
        android:textColor="@color/black"
        android:textSize="26sp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.491" />
  
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter a year to check it is leap year or not"
        android:textColor="@color/black"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.494"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.4" />
  
    <EditText
        android:id="@+id/etYear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="ENTER THE YEAR"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
  
    <Button
        android:id="@+id/btnCheck"
        android:layout_width="149dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:layout_marginTop="16dp"
        android:backgroundTint="@color/black"
        android:text="Check"
        android:textColor="@color/teal_700"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/etYear"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="@+id/etYear"
        app:layout_constraintTop_toBottomOf="@+id/etYear"
        app:layout_constraintVertical_bias="0.113" />
  
</androidx.constraintlayout.widget.ConstraintLayout>


Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail. Here we will bind the views and write the logic of the app.

Kotlin




package com.ayush.gfg_leap_year
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
  
class MainActivity : AppCompatActivity() {
  lateinit var btnCheck : Button
  lateinit var etYear : EditText
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
          
        // Binding the views
        btnCheck = findViewById(R.id.btnCheck)
        etYear = findViewById(R.id.etYear)
  
        btnCheck.setOnClickListener {
            val year = etYear.text.toString().toInt()
            if (year % 400 == 0)
                Toast.makeText(this," Leap Year ", Toast.LENGTH_LONG).show()
  
            // Else If a year is multiple of 100,
            // then it is not a leap year
            else if (year % 100 == 0)
                Toast.makeText(this,"Not A Leap Year ", Toast.LENGTH_LONG).show()
  
            // Else If a year is multiple of 4,
            // then it is a leap year
            else if (year % 4 == 0)
                Toast.makeText(this," Leap Year ", Toast.LENGTH_LONG).show()
            // else it is a leap year
            else
                Toast.makeText(this,"Not A Leap Year ", Toast.LENGTH_LONG).show()
        }
    }
}


Output:



Last Updated : 04 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads