Open In App

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

Last Updated : 23 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




<?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"
    android:background="#E3B5B5"
    tools:context=".MainActivity">
 
    <TextView
        android:id="@+id/textView2"
        android:layout_width="279dp"
        android:layout_height="51dp"
        android:text="Number Shapes"
        android:textColor="#29629A"
        android:textSize="36sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.592"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.08" />
 
    <TextView
        android:id="@+id/textView"
        android:layout_width="338dp"
        android:layout_height="47dp"
        android:text="Enter a number to check it is triangular,square,both or neither."
        android:textColor="#303F9F"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintVertical_bias="0.056" />
 
    <EditText
        android:id="@+id/editTextNumber"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Number"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.097" />
 
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="checkNumber"
        android:text="Check Number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextNumber"
        app:layout_constraintVertical_bias="0.153" />
 
</androidx.constraintlayout.widget.ConstraintLayout>


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 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. 

Java




import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
 
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
 
    class number {
         
        int num;
 
        public boolean isTriangular() {
            int a = 1;
            int triangular_number = 1;
 
            while (triangular_number < num) {
                a++;
                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(R.id.editTextNumber);
        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();
    }
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}


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

Output:

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.



Similar Reads

How to Build a Prime Number Checker Android App in Android Studio?
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 w
3 min read
How to Build a Decimal to Binary Converter Android App in Android Studio?
This app will convert a Decimal Number into a Binary Number. Some people may also have written a java program to convert a Decimal Number into Binary Number, but while making an app let's see how to do that. Brief Go through We will start by creating a new project with an Empty Activity. After creating a project, we would add the drawable resource
5 min read
How to Build Binary to Decimal Converter Android App in Android Studio?
This app will convert a Binary Number into a Decimal Number. Some people may also have written a java program to convert a Binary Number into a Decimal Number, but while making an app let's see how to do that. Brief Go Through We will start by creating a new project with an Empty Activity. After creating a project, we would add the drawable resourc
5 min read
How to Build Decimal to Hexadecimal Converter Android App in Android Studio?
In this article, we would be learning how to make a Decimal to Hexadecimal Converter App in Java in Android Studio. This app would take an input of a Decimal number, convert it into a Hexadecimal number and then show the output. So let's go through the brief go through of how we would be making the app. Brief Go through We will begin by first creat
5 min read
Build an Android App with HTML, CSS and JavaScript in Android Studio
Android is a mobile operating system based on a modified version of the Linux kernel and other open-source software, designed primarily for touchscreen mobile devices such as smartphones and tablets. Yes, you read it right in the title of this article. In this article, we are going to build an Android App with HTML, CSS, and JavaScript in Android S
2 min read
How to build a simple Calculator app using Android Studio?
Pre-requisites: Android App Development Fundamentals for BeginnersGuide to Install and Set up Android StudioAndroid | Starting with first app/android projectAndroid | Running your first Android app Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication, or division depending upon the user
7 min read
How to Build a Palindrome Checker App in Android Studio?
In this article, we will be building a Palindrome Checker android app in Android Studio using Kotlin and XML. The app will check whether the entered word is Palindrome or not, if the entered word is a Palindrome then a toast will be displayed having the message "Entered word is Palindrome" otherwise Toast's message will be "Entered word is not a Pa
3 min read
How to Build a News App Using WebView Controller in Android Studio?
In this article, we are going to make a News App with help of a WebView Controller in Android Studio. By making this application we will learn how to access Internet Permission in our android application and how to use WebView with its class named WebView Controller. After making this application you will also be aware of Navigation Drawer activity
7 min read
How to Build Fibonacci Perfect Square App using Android Studio?
Fibonacci Perfect Square App built using Android Studio is used to check whether the number entered by the user is a Fibonacci number or Perfect Square number or both. The output is displayed on SnackBar. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project usin
3 min read
How to build a simple music player app using Android Studio
This is a very simple app suitable for beginners to learn the concepts. The following pre-requisites is good enough before starting. Android fundamentals for beginnersInstall and Set up Android StudioFirst android app projectHow to Run your first Android app The following things you will learn in this article: Implementing MediaPlayer class and usi
6 min read
Article Tags :
Practice Tags :