This is a complete source code for a simple calculator. It is a very basic calculator for beginners in Android. It works fine but we can enter only two numbers simultaneously. It uses simple operators for addition, multiplication, division and subtraction.
My next post will be on calculator with more functionality using stack which is considered a good practice.
MainActivity.java
package com.example.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button button0, button1, button2, button3, button4, button5, button6,
button7, button8, button9, buttonadd, buttonsub, buttondiv,
buttondel, buttonmul, buttonequ;
EditText editText;
String str = "";
String str_sec = "";
int op1 = 0;
int op2 = 0;
String opr = "";
private int count = 0;
int ans = 0;
int z = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button0:
if (count == 0) {
str = str + "0";
editText.setText(str);
convert(str);
z++;
} else {
str_sec += "0";
convert(str_sec);
editText.setText(str + str_sec);
}
break;
case R.id.button1:
if (count == 0) {
str = str + "1";
editText.setText(str);
convert(str);
z++;
} else {
str_sec += "1";
convert(str_sec);
editText.setText(str + str_sec);
}
break;
case R.id.button2:
if (count == 0) {
str = str + "2";
editText.setText(str);
convert(str);
z++;
} else {
str_sec += "2";
convert(str_sec);
editText.setText(str + str_sec);
}
break;
case R.id.button3:
if (count == 0) {
str = str + "3";
editText.setText(str);
convert(str);
z++;
} else {
str_sec += "3";
convert(str_sec);
editText.setText(str + str_sec);
}
break;
case R.id.button4:
if (count == 4) {
str = str + "4";
editText.setText(str);
convert(str);
z++;
} else {
str_sec += "4";
convert(str_sec);
editText.setText(str + str_sec);
}
break;
case R.id.button5:
if (count == 5) {
str = str + "5";
editText.setText(str);
convert(str);
z++;
} else {
str_sec += "5";
convert(str_sec);
editText.setText(str + str_sec);
}
break;
case R.id.button6:
if (count == 0) {
str = str + "6";
editText.setText(str);
convert(str);
z++;
} else {
str_sec += "6";
convert(str_sec);
editText.setText(str + str_sec);
}
break;
case R.id.button7:
if (count == 0) {
str = str + "7";
editText.setText(str);
convert(str);
z++;
} else {
str_sec += "7";
convert(str_sec);
editText.setText(str + str_sec);
}
break;
case R.id.button8:
if (count == 0) {
str = str + "8";
editText.setText(str);
convert(str);
z++;
} else {
str_sec += "8";
convert(str_sec);
editText.setText(str + str_sec);
}
break;
case R.id.button9:
if (count == 0) {
str = str + "9";
editText.setText(str);
convert(str);
z++;
} else {
str_sec += "9";
convert(str_sec);
editText.setText(str + str_sec);
}
break;
case R.id.button_add:
if(z<=0){
Toast.makeText(getApplicationContext(), "Please enter some numbers first", Toast.LENGTH_SHORT).show();
}
else{
str = str + "+";
editText.setText(str);
opr += "+";
count++;
}
break;
case R.id.button_sub:
if(z<=0){
Toast.makeText(getApplicationContext(), "Please enter some numbers first", Toast.LENGTH_SHORT).show();
}
else{
str = str + "-";
editText.setText(str);
opr += "-";
count++;
}
break;
case R.id.button_divide:
if(z<=0){
Toast.makeText(getApplicationContext(), "Please enter some numbers first", Toast.LENGTH_SHORT).show();
}
else{
str = str + "/";
editText.setText(str);
opr += "/";
count++;
}
break;
case R.id.button_multiply:
if(z<=0){
Toast.makeText(getApplicationContext(), "Please enter some numbers first", Toast.LENGTH_SHORT).show();
}
else{
str = str + "*";
editText.setText(str);
opr += "*";
count++;
}
break;
case R.id.button_delete:
str = "";
editText.setText(str);
str = "";
str_sec = "";
count = 0;
break;
case R.id.button_equal:
operation(opr);
str = "";
str_sec = "";
opr="";
Log.e("str",str);
count = 0;
z=0;
break;
}
}
private void convert(String str2) {
if (count == 0) {
op1 = Integer.parseInt(str2);
} else {
op2 = Integer.parseInt(str2);
}
}
private void operation(String str) {
if (str.equals("+"))
ans = (op1) + (op2);
else if (str.equals("-"))
ans = (op1) - (op2);
else if (str.equals("/"))
ans = (op1) / (op2);
else if (str.equals("*"))
ans = (op1) * (op2);
String lk = "";
lk += ans;
editText.setText(lk);
}
private void initialize() {
editText = (EditText) findViewById(R.id.et);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonadd = (Button) findViewById(R.id.button_add);
buttonsub = (Button) findViewById(R.id.button_sub);
buttonmul = (Button) findViewById(R.id.button_multiply);
buttondiv = (Button) findViewById(R.id.button_divide);
buttondel = (Button) findViewById(R.id.button_delete);
buttonequ = (Button) findViewById(R.id.button_equal);
button0.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
buttonadd.setOnClickListener(this);
buttonmul.setOnClickListener(this);
buttondiv.setOnClickListener(this);
buttonsub.setOnClickListener(this);
buttondel.setOnClickListener(this);
buttonequ.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the numbers!!" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:id="@+id/button_divide"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button4"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<Button
android:id="@+id/button5"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<Button
android:id="@+id/button6"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
<Button
android:id="@+id/button_multiply"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button7"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/button8"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<Button
android:id="@+id/button9"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9" />
<Button
android:id="@+id/button_sub"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button_delete"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="DEL" />
<Button
android:id="@+id/button0"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0" />
<Button
android:id="@+id/button_equal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="=" />
<Button
android:id="@+id/button_add"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
</LinearLayout>
</LinearLayout>
0 comments:
Post a Comment