Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Develop a native calculator application


1)Open eclipse or android studio and select new android project
2)Give project name and select next
3) Choose the android version.Choose the lowest android version(Android 2.2) and select next
4) Enter the package name.package name must be two word seprated by comma and click finish
5)Go to package explorer in the left hand side.select our project.
6)Go to res folder and select layout.Double click the main.xml file
7)Now you can see the Graphics layout window.

8)Click the main.xml file and type the code below


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:layout_marginLeft="10pt"
android:layout_marginRight="10pt"
android:layout_marginTop="3pt">
<EditText
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:id="@+id/etNum1"
android:layout_width="match_parent"
android:inputType="numberDecimal">
</EditText>
<EditText
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5pt"
android:id="@+id/etNum2"
android:layout_width="match_parent"
android:inputType="numberDecimal">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2"
android:layout_marginTop="3pt"
android:layout_marginLeft="5pt"
android:layout_marginRight="5pt">
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="+"
android:textSize="15pt"
android:id="@+id/btnAdd">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="-"
android:textSize="15pt"
android:id="@+id/btnSub">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="*"
android:textSize="15pt"
android:id="@+id/btnMult">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="/"
android:textSize="15pt"
android:id="@+id/btnDiv">
</Button>
</LinearLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="5pt"
android:layout_marginRight="5pt"
android:textSize="12pt"
android:layout_marginTop="3pt"
android:id="@+id/tvResult"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>

9)Go to project explorer and select src folder.Now select mainactivity.java file and type the following code MainActivity.java coding
package CALCU.CALU;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class CALCULATORActivity extends Activity implements OnClickListener {
    EditText input1;
    EditText input2;
      Button addition;
      Button subtraction;
      Button multiplication;
      Button division;
      TextView tvResult;
      String oper = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        input1 = (EditText) findViewById(R.id.etNum1);
        input2 = (EditText) findViewById(R.id.etNum2);
        addition = (Button) findViewById(R.id.btnAdd);
        subtraction = (Button) findViewById(R.id.btnSub);
        multiplication = (Button) findViewById(R.id.btnMult);
        division = (Button) findViewById(R.id.btnDiv);
        tvResult = (TextView) findViewById(R.id.tvResult);
        // set a listener
        addition.setOnClickListener(this);
        subtraction.setOnClickListener(this);
        multiplication.setOnClickListener(this);
        division.setOnClickListener(this);
      }
      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        float num1 = 0;
        float num2 = 0;
        float result = 0;
        // check if the fields are empty
        if (TextUtils.isEmpty(input1.getText().toString())
            || TextUtils.isEmpty(input2.getText().toString())) {
          return;
        }
        // read EditText and fill variables with numbers
        num1 = Float.parseFloat(input1.getText().toString());
        num2 = Float.parseFloat(input2.getText().toString());
        // defines the button that has been clicked and performs the corresponding operation
        // write operation into oper, we will use it later for output
        switch (v.getId()) {
        case R.id.btnAdd:
          oper = "+";
          result = num1 + num2;
          break;
        case R.id.btnSub:
          oper = "-";
          result = num1 - num2;
          break;
        case R.id.btnMult:
          oper = "*";
          result = num1 * num2;
          break;
        case R.id.btnDiv:
          oper = "/";
          result = num1 / num2;
          break;
        default:
          break;
        }
        // form the output line
        tvResult.setText(num1 + " " + oper + " " + num2 + " = " + result);
      }
}
10)Now go to main.xml and right click .select run as option and select run configuration

11) Android output is present in the android emulator as shown in below. Output

2 comments:

  1. I am really thankful to all your squad for sharing specified inspirational substance.
    laminator

    ReplyDelete

Flag Counter