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.
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.
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.