Skip to content
Related Articles

Related Articles

How to Build a Number Shapes Android App in Android Studio?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 23 Jul, 2021

Android is a vast field to learn and explore and to make this learning journey more interesting and productive we should keep trying new problems with new logic. So, today we are going to develop an application in android studio which tells us about Numbers that have shapes. We will be covering two types of numbers i.e. triangular and square. So, firstly let us know what actually they are:

1. Triangular Numbers

A number that can make a triangular dot pattern is known as a triangular number. For example 1, 3, 6, 10, 15 are triangular numbers.

2. Square Numbers

A product of a number multiplied by itself is known as a square number. For example 1, 4, 9, 16, etc. In this application, we are going to whether a number is triangular, square, neither of them, or both of them. 

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 Java 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 version="1.0" encoding="utf-8"?>
        android:text="Number Shapes"
        app:layout_constraintVertical_bias="0.08" />
        android:text="Enter a number to check it is triangular,square,both or neither."
        app:layout_constraintVertical_bias="0.056" />
        android:hint="Enter Number"
        app:layout_constraintVertical_bias="0.097" />
        android:text="Check Number"
        app:layout_constraintVertical_bias="0.153" />

After writing this much code our UI looks like this:

Do not forget to link the Check Number button with the function in java code. Use the following steps to do so: 

Select the button “Check Number” and search onClick in its attributes. 

Write checkNumber there. Write logic of program in checkNumber() function in java file. 

Step 3: Working with the file 

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


import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    class number {
        int num;
        public boolean isTriangular() {
            int a = 1;
            int triangular_number = 1;
            while (triangular_number < num) {
                triangular_number = triangular_number + a;
            if (triangular_number == num) {
                return true;
            } else
                return false;
        public boolean isSquare() {
            double squareRoot = Math.sqrt(num);
            if (squareRoot == Math.floor(squareRoot))
                return true;
            else return false;
    public void checkNumber(View view) {
        String message = "";
        EditText input_number = findViewById(;
        if (input_number.getText().toString().isEmpty()) {
            message = "Please enter a number!";
        } else {
            number mynum = new number();
            mynum.num = Integer.parseInt(input_number.getText().toString());
            if (mynum.isSquare()) {
                if (mynum.isTriangular()) {
                    message = mynum.num + " is both a square and triangular number";
                } else {
                    message = mynum.num + " is a square number.";
            } else if (mynum.isTriangular()) {
                message = mynum.num + "is a triangular number.";
            } else
                message = "It is neither triangular nor a square number";
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    protected void onCreate(Bundle savedInstanceState) {

After implementing all the above steps our application runs like this:


Hence we made a basic app that tells about number shapes and by making this application we learned about triangular and square numbers, how to link our button with java code, and prepare a simple UI.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!