The Wayback Machine - https://web.archive.org/web/20240906181734/https://www.geeksforgeeks.org/how-to-build-binary-to-decimal-converter-android-app-in-android-studio/
Open In App

How to Build Binary to Decimal Converter Android App in Android Studio?

Last Updated : 09 May, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 resource file which will be used to give a box-type shape or outline to the EditText. Then we would work with the XML file where we would be adding two Edit Texts, buttons, and TextViews. In the final, we will go to the MainActivity.java and create some functions which will work when the user will click on the buttons. Note that we are going to implement the application using Java language. A sample gif is given below to get an idea of what we are going to implement in this article.

Step by Step Implementation

Step 1: Create a New Project with an Empty Activity 

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: Adding a drawable resource file

Navigate to app/res/drawable and right-click on the drawable folder and go to New/Drawable Resource File. 

Name the file as “edit_text_border” (you can name anything according to your wish but note that the letters should not be in the capital) and with the default settings, click on the ok button.

Step 3: Working with the drawable resource file

In the XML file of the drawable resource “edit_text_border.xml”, we will define the shape as a rectangle first. Then add a stroke to the shape which will make an outline around the shape. Under the same attribute, we will specify the width and color of the stroke. Finally, we will give a round shape in the corners by specifying the radius of the corners. Refer to the XML code below to understand the above lines:

XML




<?xml version="1.0" encoding="utf-8"?>
    android:shape="rectangle">
  
    <stroke
        android:width="2dp"
        android:color="@color/gfg_official"/>
  
    <corners
        android:radius="20dp"/>
  
</shape>


Step 4: Working with the activity_main.xml file

Navigate to the app/res/layout/activity_main.xml and add the below code to that file. In the activity_main.xml file, we have to add two EditText for Input and Output text to display, two buttons for submit which will submit the input and clear to reset it and I have added two TextView to show some text so that we can guide the user about how to use this app. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="200dp"
        android:layout_marginRight="8dp"
        android:background="@drawable/edit_text_border"
        android:digits="01"
        android:hint="Enter a Binary Number"
        android:inputType="numberDecimal"
        android:padding="10dp"
        android:textAlignment="center"
        android:textSize="20sp" />
  
    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText"
        android:layout_centerHorizontal="true"
        android:layout_margin="8dp"
        android:text="Submit"
        android:textAllCaps="false" />
  
    <EditText
        android:id="@+id/output"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/submit"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="40dp"
        android:layout_marginRight="8dp"
        android:background="@drawable/edit_text_border"
        android:hint="Answer will appear here"
        android:padding="10dp"
        android:textAlignment="center"
        android:textSize="20sp" />
  
    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/output"
        android:layout_centerHorizontal="true"
        android:layout_margin="8dp"
        android:text="Reset"
        android:textAllCaps="false" />
  
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/reset"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="50dp"
        android:text="⚫ Click on Submit Button after entering a Binary Number"
        android:textSize="15dp" />
  
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="⚫ Click on Reset Button to reset"
        android:textSize="15dp" />
  
</RelativeLayout>


After writing the code of the XML file for the app, the design will look something like this: 

Step 5: Working with the ActivityMain.java file

Go to the MainActivity.java file and refer to the following code. 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.Button;
import android.widget.EditText;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Giving name to the variables for two EditTexts and two Buttons
        // input is where the user will input the decimal number
        // output is where the user will get the output in the form of binary number
        // submit is the button created to submit the decimal number entered by the user
        // clear is the button to clear the answer
        EditText input, output;
        Button submit, reset;
  
        // Calling the EditText by id which we gave in xml file
        input = (EditText) findViewById(R.id.editText);
        output = (EditText) findViewById(R.id.output);
  
        submit = (Button) findViewById(R.id.submit);
  
        // It is set so that when the user clicks on submit button, the data
        // gets send in the function created below which will convert it and then
        // show the answer to the user in the output
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                // Creating a string method argument
                String string = input.getText().toString();
  
                // Here, we are parsing a string method 
                // argument into an integer object
                int i = Integer.parseInt(string, 2);
  
                // Converts and stores it in the form of string
                String decimal = Integer.toString(i);
  
                // It will show the output in the second edit text that we created
                output.setText(decimal);
            }
        });
  
        // Here, we will define a function which will
        // clear the whole text and reset it
        reset = (Button) findViewById(R.id.reset);
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                input.setText("");
                output.setText("");
            }
        });
  
    }
}


Output: 

Image

Video



Previous Article
Next Article

Similar Reads

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 creating a project, we would add the drawable resource
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 throughWe will begin by first creati
5 min read
How to Build a Number Shapes Android App in Android Studio?
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, f
4 min read
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
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 fix "Android Studio doesn't see device" in Android Studio?
In Android Studio, sometimes the list of devices and emulators doesn't list your physical device when you try to plug it in. Or you may have faced a situation when you plugged the phone in for the first time, no dialog appeared asking if you trust the computer. But the laptop is already allowed to connect to the mobile device. However, the button R
4 min read
How to Fix "Android studio logcat nothing to show" in Android Studio?
Logcat Window is the place where various messages can be printed when an application runs. Suppose, you are running your application and the program crashes, unfortunately. Then, Logcat Window is going to help you to debug the output by collecting and viewing all the messages that your emulator throws. So, this is a very useful component for the ap
2 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 Calculator app using Android Studio?
Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication, or division depending upon the user input. 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 using the Java language. [video loading="lazy" mp
6 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 prerequisites are 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
How to Build a Temperature Converter App in Android?
Temperature Converter Application is built using android studio where temperature entered in Celsius can be converted to Fahrenheit. The output will be 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 using the Java language. [video mp
3 min read
How to Add Audio Files to Android App in Android Studio?
The audio file format is a file format for saving digital audio data on a computer system and all are aware of audio files. So in this article, we are going to discuss how can we add audio files to the android app. There are three major groups of audio file formats: Format Name Description Examples Uncompressed audio formatsUncompressed audio forma
3 min read
Different Ways to Analyze APK Size of an Android App in Android Studio
APK size is one of the most important when we are uploading our app to Google Play. APK size must be considered to be as small as possible so users can visit our app and download our app without wasting so much data. In this article, we will take a look at How we can analyze our APK in Android Studio to check the APK size. Android Studio itself pro
3 min read
How to Use Stack Overflow to Fix Errors During Android App Development in Android Studio?
Stack Overflow, the life savior of developers, feels like Stack Overflow is built for the developers, by the developers, of the developers. According to Wikipedia: "Stack Overflow is a question-and-answer site for professional and enthusiast programmers". So being a developer or programmer can you imagine your life without Stack Overflow…. ? Most p
4 min read
How to Make a Scientific Calculator Android App using Android Studio?
The calculator is the app that is present on every android device. This app comes pre-installed or we can also install another application from Play Store. It is one of the most used applications for college students for making any calculations. In this article, we will take a look at building a simple scientific calculator app in Android using Kot
13 min read
How to Convert Any Website to Android App in Android Studio?
Here, we are going to make an application for the "GeeksForGeeks" website. By making this application we will be able to learn how we can convert a website to an Android Application just by following simple steps. You can use this concept for your personal website too and learn something new. What we are going to build in this article? In this appl
12 min read
Creating a Anagram Checker Android App in Android Studio with Kotlin
An anagram of a word is another word that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other. In this article, we will be building an Anagram Checker android app in Android Studio using Kotlin and XML. The app will check whether the entered words are anagrams of e
3 min read
How to Add Firebase Analytics to Android App in Android Studio?
Analytics is one of the important tools that developers should definitely used while building their applications. It is very helpful to target your apps for various audience ranges and to track on which screen the users are spending a long time. So it is one of the important features which one should not miss adding while building any Android Appli
4 min read
How to Speed Up Gradle build in Android Studio?
Gradle is one of the most important files or extensions that are present in the Android Project. Gradle handles all the libraries and application IDs and many important components of any Android application. Every time when we run our application or we build our apk we will get to see the message as Gradle build running and many more. In some devic
4 min read
How to Build a Step Counting Application in Android Studio?
Many of us have used the step counter on our phones while we go for walk or run. It counts the total steps that the user has walked and displays it on the screen. The other name for the step counter is a pedometer. But have you ever thought about how can an app count our steps? What is the coding behind the working of this app? Let's find the answe
6 min read
How to Build Age Calculator in Android Studio?
Hello geeks, today we are going to make an application to calculate Age or time period between two dates. By making this application one can calculate his/her exact age, also one can calculate the exact difference between two dates. Prerequisites: Before making this application, you can go through the article Program to calculate age to have a bett
6 min read
How to Build a Rock Paper Scissor Game in Android Studio?
Rock Paper Scissor (which is also called Stone Paper Scissor) is a hand game and played between two people, in which each player simultaneously forms one of three shapes. The winner of the game is decided as per the below rules: Rock vs Paper -&gt; Paper wins.Rock vs Scissor -&gt; Rock wins.Paper vs Scissor -&gt; Scissor wins. In this game, the use
6 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 out BMI we will take input from the user (both heigh
4 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 number will convert into its corresponding roman nume
5 min read
How to Build a SOS Mobile Application in Android Studio?
The SOS applications are basically advanced emergency apps that can rescue you and/or your loved ones if you and/or they find themselves in a life-threatening emergency situation and need immediate assistance. When you need some personal assistance, you can actually turn on your phone and can call or message someone for help. But in a life-threaten
15+ min read
Android Java Projects - Build Your First Android App from Scratch
Are you eager to boost your Android development skills and create cool projects? Our blog post, "Android Java Projects," is here to help! Whether you're just starting your software development career or want to add good projects to your CV then, working on real projects is the best way to learn and grow as a developer. In this post, we've gathered
6 min read
Fix "Execution Failed for task :app:compileDebugJavaWithJavac" in Android Studio
Working on some important java project with Java code involved, and just when you hit that Build hammer icon, you get this: Error:Execution failed for task ':app:compileDebugJavaWithJavac'. &gt; Compilation failed; see the compiler error output for details. * What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'. &gt; Compilat
2 min read
Different Ways to fix "Execution failed for task ':app:clean'. Unable to delete file" error in Android Studio
When you try to rebuild or clean and build your project (containing mostly Kotlin code) in Android Studio, it fails and throws the error: Execution failed for task ':app:clean'. Unable to delete file: SpecificFileLocation So, In this article, we are going to see the topics: why there is a need to solve this error and how to solve this error. We wil
3 min read
Practice Tags :