Click here to Skip to main content
13,507,590 members
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 18 Oct 2012

Number System Converter Android App

, 18 Oct 2012
Rate this:
Please Sign up or sign in to vote.
This article demonstrates creating an Android application to perform number system conversion.

Sample Image

Sample Image


Android is an exciting platform for developing apps that run on a mobile device. Any programmer familiar with Java can learn Android quickly and start developing Android apps. The class library of Android is a subset of Java SE. The two main elements of Android are the Android SDK and an Eclipse plug-in called 'The Android Development Tools (ADT)'. You must ensure the appropriate installation of the SDK and ADT before starting development.

In this article, I am describing how to create an Android app to perform conversion of a decimal number to binary, octal and hexadecimal formats.


There are four building blocks of any android application:

  • Activities: The basic building block of a user interface in Android is an activity. An activity represents the main screen with which a user can interact with an application.
  • Content Providers: Content providers provide abstraction for data required by multiple applications.
  • Intents: Intents are system messages, for e.g., event notifications, for e.g., hardware changes (like SD card insertion), incoming data (like SMS), etc.
  • Services: Services are independent activities not having an interface for e.g., music playback.

Activities are public classes derived from the base class. The following code represents an activity in the application:

public class MyActivity extends Activity
   public void onCreate(Bundle savedInstanceState)

The onCreate() method gets invoked when the activity is started. The first statement in this method must be to invoke the base classes' onCreate() method to initialize the Android activity.

User interfaces in Android can be created either through Java code or XML files. Using XML files for creating user interfaces is highly preferred because it provides separation of the user interface code from the business logic code.

Following is an example of a LinearLayout definition.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""




Every android project has an auto-generated file. This file is used to refer to resources in your project. For e.g., if the main.xml layout file defines the following resource:

<Button android:text="My Button" android:id="@+id/button1" 

   android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

then it can be used in the activity class by writing the following code:

Button btn=(Button)findViewById(;

Events in android are handled in the same way as in Java SE. For e.g., an activity class can implement the View.onClickListener interface, having the onClick() method to handle the click event of a button. Similarly, the TextWatcher interface can be used to handle the text changed event in an EditText control. The TextWatcher interface has three methods, beforeTextChanged(), afterTextChanged(), and onTextChanged().

User Interface components in Android are represented by Views. The View class is the base class of all components. Some of the views are TextView, EditText, Button, RadioButton and Checkbox. View can respond to events using listeners. Assigning listeners to Views is done in the same way as in Java 2 SE.

For e.g., the following code sets the listener for a Button to respond to the click event:


The following code sets the listener for an EditText to respond to the text changed event:


Dialog boxes are created in android using the Builder class. The Builder class has the following methods:

  • setTitle(): To set title for the dialog
  • setMessage(): To set message for the dialog
  • setCancelable(): To allow/disallow cancelling the dialog
  • show(): To display the dialog

The following code can be used to create a dialog box and display it.

Builder builder=new Builder(this);

Using the code

In my application, I have created an EditText control having id txtDecimal to accept a number in decimal format and three TextView controls having IDs txtBinary, txtOctal and txtHexadecimal to display the numbers in binary, octal and hexadecimal formats.

Following is the complete code of the main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""




    <TextView android:text="Enter Decimal Number: " android:id="@+id/textView1" 

      android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:text="" android:id="@+id/txtDecimal" android:layout_width="match_parent" 

    <TextView android:text="Binary: " android:id="@+id/textView2" 

      android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <TextView android:text="" android:id="@+id/txtBinary" 

      android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <TextView android:text="Octal: " android:id="@+id/textView4" 

      android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <TextView android:text="" android:id="@+id/txtOctal" android:layout_width="wrap_content" 

    <TextView android:text="Hexadecimal: " android:id="@+id/textView6" 

      android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <TextView android:text="" android:id="@+id/txtHexadecimal" 

      android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <Button android:text="About" android:id="@+id/button1" 

      android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

Following is the code of the main activity:

package com.azim;

import java.util.Stack;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.text.Editable;
import android.text.TextWatcher;

public class MyActivity extends Activity implements TextWatcher,View.OnClickListener
    EditText txtDecimal;
    TextView txtBinary,txtOctal,txtHexadecimal;
    Button btnAbout;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    public void beforeTextChanged(CharSequence sequence,int start,int count,int after)
    public void afterTextChanged(Editable editable)
    public void onTextChanged(CharSequence sequence,int start,int before,int count)
        calculate(2,txtBinary);        // for base 2 (binary)
        calculate(8,txtOctal);        // for base 8 (octal)
        calculate(16,txtHexadecimal);    // for base 16 (hexadecimal)
    public void calculate(int base,TextView txtView)
            Stack<Object> stack=new Stack<Object>();
            int number=Integer.parseInt(txtDecimal.getText().toString());
            while (number>0)
                int remainder=number%base; // find remainder
                // for remainder smaller than 10
                    // push remainder in stack
                    switch (remainder)
                    // for remainder larger than 9 (for hexadecimal values)
                    case 10:
                    case 11:
                    case 12:
                    case 13:
                    case 14:
                    case 15:
        StringBuffer buffer=new StringBuffer();
        while (!stack.isEmpty())
       catch (Exception e)

public void onClick(View view)
// to display Information in a dialog box
   // create a dialog box
   Builder builder=new Builder(this);
   // to allow cancelling the dialog box
   // set title
   builder.setTitle("About NumberSystemConverter");
   // set message
   builder.setMessage("Made by Azim");
   // display dialog box;

The above code uses the onTextChanged() method to get the decimal number entered by the user and call the user-defined method called calculate() to convert the decimal number to binary, octal and hexadecimal formats. The onTextChanged() method gets executed whenever the text is changed in an EditText control. The calculate() method takes two parameters. The first parameter denotes the base of the target number system and the second parameter denotes the target TextView control on which to display the result.

After executing the program, the following screen appears:

Clicking the About button displays the following screen:

Points of Interest

Executing the program generates the NumberSystemConverter.apk file in the bin folder. This file can be copied to an android device in order to execute the app on the device. The following image shows the app being executed on my HTC Explorer android mobile:

I hope my article will be helpful to developers new to the android platform.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Azim Zahir
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with NIIT (Mumbai, India) as a Senior Faculty. I enjoy programming as a hobby. My favorite technologies are Flash, Flex and Silverlight.

Of late I have developed keen interest in WPF and Windows Mobile programming.

Apart from computers, my favorite pastime is bicycling.

You may also be interested in...


Comments and Discussions

GeneralGood one! Pin
Member 1109084117-Sep-14 20:01
memberMember 1109084117-Sep-14 20:01 
GeneralRe: Good one! Pin
Azim Zahir19-Sep-14 19:15
memberAzim Zahir19-Sep-14 19:15 
Questionhelp needed Pin
Anurag Sinha V24-Aug-14 5:53
memberAnurag Sinha V24-Aug-14 5:53 
Questionhelp me out.. Pin
Member 103869839-Nov-13 5:31
memberMember 103869839-Nov-13 5:31 
AnswerRe: help me out.. Pin
Azim Zahir17-Nov-13 18:13
memberAzim Zahir17-Nov-13 18:13 
Questionasking about logarithm Pin
Member 1033411514-Oct-13 21:24
memberMember 1033411514-Oct-13 21:24 
AnswerRe: asking about logarithm Pin
Azim Zahir16-Oct-13 15:59
memberAzim Zahir16-Oct-13 15:59 
Questionres>layout values Pin
danskieness4-Mar-13 1:59
memberdanskieness4-Mar-13 1:59 
AnswerRe: res>layout values Pin
Azim Zahir11-Mar-13 19:22
memberAzim Zahir11-Mar-13 19:22 
Questionone problem encountered... Pin
anurag34871-Nov-12 20:07
memberanurag34871-Nov-12 20:07 
AnswerRe: one problem encountered... Pin
Azim Zahir2-Nov-12 16:33
memberAzim Zahir2-Nov-12 16:33 
GeneralRe: one problem encountered... Pin
anurag34873-Nov-12 6:24
memberanurag34873-Nov-12 6:24 
GeneralRe: one problem encountered... Pin
anurag34875-Nov-12 7:00
memberanurag34875-Nov-12 7:00 
GeneralRe: one problem encountered... Pin
Azim Zahir5-Nov-12 17:37
memberAzim Zahir5-Nov-12 17:37 
Questionnice crisp article!! Pin
anurag348731-Oct-12 21:09
memberanurag348731-Oct-12 21:09 
AnswerRe: nice crisp article!! Pin
Azim Zahir2-Nov-12 16:50
memberAzim Zahir2-Nov-12 16:50 
GeneralMy vote of 5 Pin
hari1911319-Oct-12 2:19
memberhari1911319-Oct-12 2:19 
GeneralRe: My vote of 5 Pin
Azim Zahir19-Oct-12 3:56
memberAzim Zahir19-Oct-12 3:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180417.1 | Last Updated 18 Oct 2012
Article Copyright 2012 by Azim Zahir
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid