In this blog implement the Basic calculator, try this code
Step 1:
Create a activity_main.xml inside res/layout folder....
activity_main.xml
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10pt"
android:layout_marginRight="10pt"
android:layout_marginTop="3pt" >
<EditText
android:id="@+id/editNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:layout_weight="1"
android:inputType="numberDecimal" >
</EditText>
<EditText
android:id="@+id/editNumbers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5pt"
android:layout_weight="1"
android:inputType="numberDecimal" >
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5pt"
android:layout_marginRight="5pt"
android:layout_marginTop="3pt" >
<Button
android:id="@+id/btnAddtion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="8pt" >
</Button>
<Button
android:id="@+id/btnSubtract"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="8pt" >
</Button>
<Button
android:id="@+id/btnMultiply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="8pt" >
</Button>
<Button
android:id="@+id/btnDivision"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="8pt" >
</Button>
</LinearLayout>
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5pt"
android:layout_marginRight="5pt"
android:layout_marginTop="3pt"
android:gravity="center_horizontal"
android:textSize="12pt" >
</TextView>
</LinearLayout>
Step:2
Create MainActivity.java inside src folder
Main_Activity.java
..................................................................................................................................................................
package com.example.androidbeginnerpoin;
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 MainActivity extends Activity implements OnClickListener {
EditText editNum1;
EditText editNum2;
Button buttionAdd;
Button buttionSub;
Button buttionMult;
Button buttionDiv;
TextView textResult;
String oper = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editNum1 = (EditText) findViewById(R.id.editNumber);
editNum2 = (EditText) findViewById(R.id.editNumbers);
buttionAdd = (Button) findViewById(R.id.btnAddtion);
buttionSub = (Button) findViewById(R.id.btnSubtract);
buttionMult = (Button) findViewById(R.id.btnMultiply);
buttionDiv = (Button) findViewById(R.id.btnDivision);
textResult= (TextView) findViewById(R.id.tvResult);
buttionAdd.setOnClickListener((OnClickListener) this);
buttionSub.setOnClickListener(this);
buttionMult.setOnClickListener(this);
buttionDiv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
float num1 = 0;
float num2 = 0;
float result = 0;
if (TextUtils.isEmpty(editNum1.getText().toString())
|| TextUtils.isEmpty(editNum2.getText().toString())) {
return;
}
num1 = Float.parseFloat(editNum1.getText().toString());
num2 = Float.parseFloat(editNum2.getText().toString());
switch (v.getId()) {
case R.id.btnAddtion:
oper = "+";
result = num1 + num2;
break;
case R.id.btnSubtract:
oper = "-";
result = num1 - num2;
break;
case R.id.btnMultiply:
oper = "*";
result = num1 * num2;
break;
case R.id.btnDivision:
oper = "/";
result = num1 / num2;
break;
default:
break;
}
textResult.setText(num1 + " " + oper + " " + num2 + " = " + result);
}
}
0 comments