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\com.pcsalt.math.app\MathAppActivity.java and insert following code:
    package com.pcsalt.math.app;
    
    import android.os.Bundle;
    import android.app.Activity;
    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;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_math_app);
    
            //Mapping of XML layout buttons with JAVA Button Class objects
            btnAdd = (Button) findViewById(R.id.btnAdd);
            btnSub = (Button) findViewById(R.id.btnSub);
            btnMul = (Button) findViewById(R.id.btnMul);
            btnDiv = (Button) findViewById(R.id.btnDiv);
            btnMod = (Button) findViewById(R.id.btnModule);
            etNum1 = (EditText) findViewById(R.id.etNum1);
            etNum2 = (EditText) findViewById(R.id.etNum2);
            tvResult = (TextView) findViewById(R.id.tvResult);
    
            btnAdd.setOnClickListener(this);
            btnSub.setOnClickListener(this);
            btnMul.setOnClickListener(this);
            btnDiv.setOnClickListener(this);
            btnMod.setOnClickListener(this);
    
        }
    
    	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()) {
    		case R.id.btnAdd:
    			result = num1 + num2;
    			break;
    		case R.id.btnSub:
    			result = num1 - num2;
    			break;
    		case R.id.btnMul:
    			result = num1 * num2;
    			break;
    		case R.id.btnDiv:
    			result = num1 / num2;
    			break;
    		case R.id.btnModule:
    			result = num1 % num2;
    			break;
    		//default case is not needed as we don't have any
    		//condition to invoke default case.
    		}
    		tvResult.setText(String.valueOf(result));
    	}
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_math_app, 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