Open In App

How to Build a Prime Number Checker Android App in Android Studio?

Last Updated : 09 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A prime number is a natural number greater than 1, which is only divisible by 1 and itself. First few prime numbers are : 2 3 5 7 11 13 17 19 23 ….. . In this article, we will be building a Prime Number Checker android app in Android Studio using Kotlin and XML. The app will check whether the entered number is a Prime number or not, if the entered word is a Prime number then a Toast will be displayed having the message “Entered Number is a Prime Number” otherwise Toast’s message will be “Entered word is not a Prime Number”.

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.

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
<?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"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Enter a Number"
        android:textSize="24sp" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:textAlignment="center"
        android:layout_marginTop="32dp"
        android:layout_marginStart="64dp"
        android:layout_marginEnd="64dp"
        android:layout_gravity="center_horizontal" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:layout_gravity="center_horizontal"
        android:text="Check" />

</LinearLayout>


Design UI:

prime-number-checker--main


Step 4: Working with the MainActivity file

Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail. 

MainActivity.java
package org.geeksforgeeks.demo;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText numberInput;
    private Button verifyButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Reference views
        numberInput = findViewById(R.id.editText);
        verifyButton = findViewById(R.id.button);

        // Set listener on button click
        verifyButton.setOnClickListener(v -> {
            String input = numberInput.getText().toString().trim();

            // Check if input is not empty
            if (!input.isEmpty()) {
                try {
                    int num = Integer.parseInt(input);
                    if (isPrime(num)) {
                        showToast("It is a Prime Number");
                    } else {
                        showToast("Not a Prime Number");
                    }
                } catch (NumberFormatException e) {
                    showToast("Invalid number");
                }
            } else {
                showToast("Enter a number");
            }
        });
    }

    // Prime number check
    private boolean isPrime(int num) {
        if (num <= 1) return false;
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) return false;
        }
        return true;
    }

    // Show toast message
    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}
MainActivity.kt
package org.geeksforgeeks.demo

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.text.isDigitsOnly
import kotlin.math.sqrt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get reference to all views
        val numberInput = findViewById<EditText>(R.id.editText)
        val verifyButton = findViewById<Button>(R.id.button)

        // when user clicks on check button.
        verifyButton.setOnClickListener {
            
            // check if input is empty or not
            if (numberInput.text.isNotEmpty()) {
                val num = numberInput.text.toString().toInt()
                
                // check if num is a prime number or not.
                if (isPrime(num)) {
                    showToast("It is a Prime Number")
                } else {
                    showToast("Not a Prime Number")
                }
            }
            
            // when input is empty.
            else {
                showToast("enter a number")
            }
        }
    }

    // function to check is a number is prime or not.
    private fun isPrime(num: Int): Boolean {
        
        // Corner case
        if (num <= 1) return false

        // Check from 2 to square root of n
        for (i in 2..sqrt(num.toDouble()).toInt()) if (num % i == 0) return false
        return true
    }

    // function to show toast message
    private fun showToast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }
}


Output:




Next Article

Similar Reads