Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Last Updated : 19 Sep, 2021
View Discussion
Improve Article
Save Article

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. Note that select Kotlin as the programming language.

Step 2: Working with the build.gradle(Module) File

You need to apply the plugin kotlin-android-extensions in app build.gradle module like this

plugins {

   id ''

   id 'kotlin-android'

   id 'kotlin-android-extensions'


Step 3: 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 version="1.0" encoding="utf-8"?>
        android:text="Prime Number Checker"
        app:layout_constraintTop_toTopOf="parent" />
        android:text="Enter a Number"
        android:textStyle="bold" />
        android:layout_marginTop="50dp" />
        android:textColor="@color/white" />

After writing this much code our UI looks like this:

Step 4: 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. 


import android.os.Bundle
import android.widget.Toast
import androidx.core.text.isDigitsOnly
import android.R.bool
import java.lang.Math.sqrt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // when user clicks on check button.
        check_btn.setOnClickListener {
            // check if number_et.text is empty or not
            if (number_et.text.isNotEmpty()) {
                // check if et.text is an integer or not.
                if (number_et.text.isDigitsOnly()) {
                    val num = number_et.text.toString().toInt()
                    // check if num is a prime number or not.
                    if (CheckPrime(num)) {
                        Toast.makeText(this, "Entered number is a Prime Number.", Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(this, "Entered number is a not a Prime Number.", Toast.LENGTH_SHORT).show()
                // when number_et.text is not an integer value.
                else {
                    Toast.makeText(this, "please enter a valid number.", Toast.LENGTH_SHORT).show()
            // when number_et.text is empty.
            else {
                Toast.makeText(this, "please enter a number", Toast.LENGTH_SHORT).show()
    // function to check is a number is prime or not.
    private fun CheckPrime(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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!