How to Build a Number Shapes Android App in Android Studio?
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
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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: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 entere
3 min read
How to Build a Sensor App in Android?
Android mobile phones have sensors, so we can perform various functions like Controlling screen brightness, Monitoring acceleration along a single axis, Motion detection, etc. In this article, we will be building an application that will determine the intensity of light in the room with the help of
5 min read
How to Build a Simple Notes App in Android?
Notes app is used for making short text notes, updating when you need them, and trashing when you are done. It can be used for various functions as you can add your to-do list in this app, some important notes for future reference, etc. The app is very useful in some cases like when you want quick a
9 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 Java/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 "Yes, it's a palindrome" otherwis
3 min read
How to Build a Roman Numeral Convertor in Android Studio?
Roman Numeral converter is an app through which we can convert a decimal number to its corresponding roman number or a roman number to its corresponding decimal number in the range of 1 to 3999. The user will enter a decimal number and on clicking the convert to roman numeral button, the entered num
5 min read
How to Make a Meme Sharing App in 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 In this article, we will learn how to create an app using Kotlin where memes will be fetched from Redd
5 min read
How to Build a Body Mass Index Calculator in Android Studio?
The Body Mass Index (BMI) Calculator can be used to calculate BMI values based on height and weight. BMI is a fairly reliable indicator of body fatness for most people. Formula: BMI = (weight) / (height * height) Approach: BMI is a number calculated from an individualâs weight and height. To find ou
4 min read
How to Build a Sudoku Solver Android App?
In this article, we are going to make a sudoku solver Android app in Kotlin and xml. The main focus of the app will be on the sudoku-solving algorithm so the the design part has been hardcoded. A sample video is given below to get an idea about what we are going to do in this article. Step by Step I
12 min read
How to Add Resource File in Existing Android Project in Android Studio?
Android resource file is a file that stores resources needed for the UI part of an android application. Like: Layout files, drawable files, etc. In the android application, it is very often that we need another resource file for another purpose in the existing application. There are many types of re
1 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 throughWe will start by creating a new project with an Empty Activity. After creati
5 min read