Build an Android App to Check a Year is Leap Year or Not
In this program, you’ll learn to check if the given year is a leap year or not. This is checked using a if statement on android studio.
Before that, we need to have an idea of what is a leap year? If a year is:
- Multiple of 400
- multiple of 4 and not multiple of 100
Algorithm
- if the year is divisible by 4 then it is a leap year.
- else it is not a leap year.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio. Note that select Java as the programming language. Select a minimum API that can support more than 98.0% android device. I recommend API 21: Lollipop version.
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.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" tools:context=".MainActivity" android:orientation="vertical" android:padding="15dp" android:background="@drawable/nback" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Leap Year Checker" android:textSize="25sp" android:textStyle="bold" android:textColor="#000000" android:gravity="center" android:padding="10dp" android:layout_marginTop="30dp" /> <EditText android:id="@+id/edText" android:layout_marginTop="30dp" android:layout_width="300dp" android:layout_height="wrap_content" android:hint="Enter a Year. Expl- 2004" android:layout_gravity="center" android:inputType="number" /> <Button android:id="@+id/button" android:layout_width="150dp" android:layout_height="wrap_content" android:text="Submit" android:layout_gravity="center" android:padding="10dp" android:layout_marginTop="25dp" android:backgroundTint="#FF0761" android:textColor="#000000" android:textStyle="bold" /> <TextView android:id="@+id/display" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_gravity="center" android:layout_marginTop="25dp" android:textColor="#0637E8" android:textStyle="bold" android:textSize="20dp" /> </LinearLayout>
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java 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.
Java Program to Check Leap Year
MainActivity.java
package com.blogguru99.leapyearchecker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button;
EditText edtext;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ID Submit
button = findViewById(R.id.button);
edtext = findViewById(R.id.edText);
display = findViewById(R.id.display);
// button Action
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// get year
String sedtext = edtext.getText().toString();
if ( sedtext.length() == 4 ) {
int year = Integer.parseInt(sedtext);
if ( year % 4 == 0 ) {
display.setText(year + " is a Leap Year");
}else {
display.setText(year + " is not a Leap Year");
}
}else {
Toast.makeText(MainActivity.this, " Please Enter a Year", Toast.LENGTH_SHORT).show();
edtext.setError("Please Enter a Year");
}
}
});
}
}
Download Full Source File
Download apk file
Download Background Image