Math App – Android Application

Home / Android / Math App – Android Application

Let us create an application to perform mathematical operations using android application. For this purpose we have to do following task:

  • Create GUI components with TextView, EditText, and Button, and
  • Write JAVA code for performing mathematical tasks.

In this application we will have functionality for addition, subtraction, division, multiplication, and modulus. To program these functionality XML parsing and Event Handling are used. A little knowledge of JAVA or any OOP language may prove helpful. You can follow the steps to create this application.

  1. Create android project by name Math App,

    Math App
    Create project

    Math App
    Launcher Icon

    Math App
    Select Activity

    Math App
    Activity Name
  2. Eclipse will create the project and open the GUI editor for application’s XML
    Math App
    Layout XML

    Delete the existing Hello World! TextView. Then, change the layout to Vertical Layout. For this, right click the GUI layout and select Change Layout. A dialog box will appear select LinearLayout (Vertical).

  3. The following process can be done by coding as well as drag and drop. Now, from the pallet you can drag and drop the contents, but here we are using coding. Add following code:
    Math App
    GUI of Math App

    If you run this app now you will have this view. Don’t worry about the yellow underlines you are having in XML view. If you put all the text and hint in res/values/strings.xml then the warnings will go away.

  4. Next thing is to provide functionality to these buttons. This will be done by JAVA file. Open Math App\src\\ and insert following code:
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    public class MathAppActivity extends Activity implements OnClickListener {
    	Button btnAdd,btnSub,btnMul,btnDiv,btnMod;
    	EditText etNum1,etNum2;
    	TextView tvResult;
        public void onCreate(Bundle savedInstanceState) {
            //Mapping of XML layout buttons with JAVA Button Class objects
            btnAdd = (Button) findViewById(;
            btnSub = (Button) findViewById(;
            btnMul = (Button) findViewById(;
            btnDiv = (Button) findViewById(;
            btnMod = (Button) findViewById(;
            etNum1 = (EditText) findViewById(;
            etNum2 = (EditText) findViewById(;
            tvResult = (TextView) findViewById(;
    	public void onClick(View v) {
    		int num1 = Integer.parseInt(etNum1.getText().toString());
    		int num2 = Integer.parseInt(etNum2.getText().toString());
    		int result = 0;
    		switch(v.getId()) {
    			result = num1 + num2;
    			result = num1 - num2;
    			result = num1 * num2;
    			result = num1 / num2;
    			result = num1 % num2;
    		//default case is not needed as we don't have any
    		//condition to invoke default case.
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(, menu);
            return true;
  5. Now, we are done with programming and our application is ready to run. To run the application
    Right Click “Math App” > Run As > Android Project (If asked then select the emulator/android device to run the project).

    Math App - Running
    Math App – Running