You can view new updated simple calculator with ViemModel and LiveData in my new blog
https://androidtuts4u.blogspot.com/2021/10/simple-calculator-with-viewmodel-and.html
To create a calculator first we need to create the layout of the calculator.
Layout is created using XML file given below
The java file is given below:
To create a calculator first we need to create the layout of the calculator.
Layout is created using XML file given below
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:id="@+id/result_id" android:layout_width="fill_parent" android:layout_height="120dp" /> <Button android:id="@+id/Btn7_id" android:layout_width="70dp" android:layout_height="60dp" android:layout_below="@id/result_id" android:text="7" android:onClick="btn7Clicked" /> <Button android:id="@+id/Btn8_id" android:layout_width="70dp" android:layout_height="60dp" android:layout_below="@id/result_id" android:layout_toRightOf="@id/Btn7_id" android:text="8" android:onClick="btn8Clicked" /> <Button android:id="@+id/Btn9_id" android:layout_width="70dp" android:layout_height="60dp" android:layout_below="@id/result_id" android:layout_toRightOf="@id/Btn8_id" android:text="9" android:onClick="btn9Clicked" /> <Button android:id="@+id/Btnclear_id" android:layout_width="90dp" android:layout_height="60dp" android:layout_below="@id/result_id" android:layout_toRightOf="@id/Btn9_id" android:text="clear" android:onClick="btnclearClicked" /> <Button android:id="@+id/Btn4_id" android:layout_width="70dp" android:layout_height="60dp" android:layout_below="@id/Btn7_id" android:text="4" android:onClick="btn4Clicked" /> <Button android:id="@+id/Btn5_id" android:layout_width="70dp" android:layout_height="60dp" android:layout_below="@id/Btn8_id" android:layout_toRightOf="@id/Btn4_id" android:text="5" android:onClick="btn5Clicked" /> <Button android:id="@+id/Btn6_id" android:layout_width="70dp" android:layout_height="60dp" android:layout_below="@id/Btn9_id" android:layout_toRightOf="@id/Btn5_id" android:text="6" android:onClick="btn6Clicked" /> <Button android:id="@+id/Btnplus_id" android:layout_width="90dp" android:layout_height="60dp" android:layout_below="@id/Btnclear_id" android:layout_toRightOf="@id/Btn6_id" android:text="+" android:onClick="btnplusClicked" /> <Button android:id="@+id/Btn1_id" android:layout_width="70dp" android:layout_height="60dp" android:layout_below="@id/Btn4_id" android:text="1" android:onClick="btn1Clicked" /> <Button android:id="@+id/Btn2_id" android:layout_width="70dp" android:layout_height="60dp" android:layout_below="@id/Btn5_id" android:layout_toRightOf="@id/Btn1_id" android:text="2" android:onClick="btn2Clicked" /> <Button android:id="@+id/Btn3_id" android:layout_width="70dp" android:layout_height="60dp" android:layout_below="@id/Btn6_id" android:layout_toRightOf="@id/Btn2_id" android:text="3" android:onClick="btn3Clicked" /> <Button android:id="@+id/Btnminus_id" android:layout_width="90dp" android:layout_height="60dp" android:layout_below="@id/Btnplus_id" android:layout_toRightOf="@id/Btn3_id" android:text="-" android:onClick="btnminusClicked" /> <Button android:id="@+id/Btnequal_id" android:layout_width="110dp" android:layout_height="60dp" android:layout_below="@id/Btn1_id" android:text="=" android:onClick="btnequalClicked" /> <Button android:id="@+id/Btndivide_id" android:layout_width="90dp" android:layout_height="60dp" android:layout_below="@id/Btn1_id" android:layout_toRightOf="@id/Btnequal_id" android:text="/" android:onClick="btndivideClicked" /> <Button android:id="@+id/Btnmulti_id" android:layout_width="90dp" android:layout_height="60dp" android:layout_below="@id/Btnminus_id" android:layout_toRightOf="@id/Btndivide_id" android:text="*" android:onClick="btnmultiClicked" /> </RelativeLayout>
The java file is given below:
package com.arun.calculator; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { public String str =""; Character op = 'q'; int i,num,numtemp; EditText showResult; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); showResult = (EditText)findViewById(R.id.result_id); } public void btn1Clicked(View v){ insert(1); } public void btn2Clicked(View v){ insert(2); } public void btn3Clicked(View v){ insert(3); } public void btn4Clicked(View v){ insert(4); } public void btn5Clicked(View v){ insert(5); } public void btn6Clicked(View v){ insert(6); } public void btn7Clicked(View v){ insert(7); } public void btn8Clicked(View v){ insert(8); } public void btn9Clicked(View v){ insert(9); } public void btnplusClicked(View v){ perform(); op = '+'; } public void btnminusClicked(View v){ perform(); op = '-'; } public void btndivideClicked(View v){ perform(); op = '/'; } public void btnmultiClicked(View v){ perform(); op = '*'; } public void btnequalClicked(View v){ calculate(); } public void btnclearClicked(View v){ reset(); } private void reset() { // TODO Auto-generated method stub str =""; op ='q'; num = 0; numtemp = 0; showResult.setText(""); } private void insert(int j) { // TODO Auto-generated method stub str = str+Integer.toString(j); num = Integer.valueOf(str).intValue(); showResult.setText(str); } private void perform() { // TODO Auto-generated method stub str = ""; numtemp = num; } private void calculate() { // TODO Auto-generated method stub if(op == '+') num = numtemp+num; else if(op == '-') num = numtemp-num; else if(op == '/') num = numtemp/num; else if(op == '*') num = numtemp*num; showResult.setText(""+num); } }
Thanks you so much. This is the simplest Calculator code even i have seen. thank you so much for this wonderful code. Highly recommended.
ReplyDeletecould you please let me know how to add (.dot) on this calculater
ReplyDeleteIT IS NOT NECESSARY TO ADD (.) AS A BUTTON .JUST ADD IN EDIT TEXT OF XML- android:inputType=numberDecimal; numberDecimal will accept . values
DeleteHow to solve this error:
ReplyDeleteNo grammar constraints (DTD or XML schema) detected for the document.
on line <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
please add <?xml version="1.0" encoding="utf-8"? this on top of <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android",let me know if the error persists..
Deleteplz tell me where these methods are called that are defined in the source code?
DeleteHi..am getting this problem each time am trying to run the app :
ReplyDelete`main cannot be resolved or is not a field`
The code with the `main` is as follows :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HELP PLZ :(
your main.xml file has error , correct it and rebuild the project, give me your emailId i will send you the complete sourcecode
DeleteThis comment has been removed by the author.
DeleteIf you can also send me the complete code... lllach@gmail.com. Thx.
DeleteHEY MEN U CAN CHANG setContentView(R.layout.main);
DeleteAND ADD setContentView(R.layout.activity_main);
activity_main IS NAME FOR YOUR ACTIVITY :)
plz send me the com plete code.. i am getting same error
Deletemy id is nupurchoudhary12@gmail.com
Plz send me the code kamesh0005@gmail.com
Deletei am getting the error
Deletemain cannot be resolved or is not a field`
&
Error:(19, 49) error: cannot find symbol variable showResult
please send me the code
my email id is atulmishra1996@gmail.com
please sent the full code of this calculater program
Deleteplease sent the full code of this calculater program
DeleteThanks a lot...it worked finally. It was very much helpful for me.
ReplyDeletehello ...when I tried to run the program in avd ..it showed the message "Unfortunately Simple calculator has stopped"..need help please
ReplyDeletegive me u r email id i will sent you the complete source code.
DeleteThis comment has been removed by the author.
DeleteSujit.singh358@gmail.com
DeleteSujit.singh358@gmail.com
Deleteplease send da code
Deletesavnish09@gmail.com
send me full code , if anyone have ,
Deletealonesufyan03@gmail.com
Really great stuff i find after many tedious programs of calculator.
ReplyDeletereally great program.easy to understand
I also get an error in
ReplyDeletesetContentView(R.layout.main);
hw do i add a dot
ReplyDeletethis program uses int variables so first you should change int to float or double then add '.' button like 1,2,3.. ,
Deleteon clicking add button call insert(),
Hi Arun,
Deleteplz send me the complete source code.
my E-Mail id is jaydeepbldn72@gmail.com
thank u.....
Please send me the source code of adding dot. - benylroger@yahoo.com
Deletewhy it is displaying the message "Unfortunately Simple calculator has stopped"..need help please
ReplyDeletegive me u r email id i will sent yo the complete source code..
DeleteHi Arun,
Deleteplz send me the complete source code.
my E-Mail id is akkichaudhary29@gmail.com
thank u.....
i don't find the number "0" button ... help me out ...
ReplyDeletesorry,i think i forgot to add button 0 , add it in your code just like other buttons .
Deletei got "Unfortunately Simple calculator has stopped" so please help
ReplyDeletehey i am getting some problem while running the java code in eclipse IDE ... when i run the code it ask whether u want to run it by java application or java applet, i select the both options one by one but then it says "Selection does not contain a main type" ..
ReplyDeleteplease help me out
please give me your e-mail id, i will send you the complete source cod.
Deletefeeha.macky@gmail.com
DeleteThis comment has been removed by the author.
ReplyDeleteplease give the full code
ReplyDeletemy email id: iambinayakc@gmail.com
can i also have the source code please, jeffreypapali@hotmail.com
ReplyDeletehelo sir i m nt getting any errors while running but wen i enter a number and aftr dat press any operator(+,-,*,/) then its nt performing ant task,,plz help..:(
ReplyDeletehi arun can you also send me the full code.. shibucai11@gmail.com
ReplyDeletethanks!!
Hi! I didn't make an account so can we please keep contact through email ? Mine's asimesoof@gmail.com. My question is how do you have your own colour numpad in the app? For example, like the app: Flib... Also, can you please tell me how to use source codes for converters from websites and include them in the app ??? Thanks in advance & Keep up the good work!!!
ReplyDeleteCan I have the source code also my email is nlrushin@gmail.com
ReplyDeleteTsegay please send the full code to my email tsegay169@gmail.com
ReplyDeletebut I admire you for solving that problem!Thanks More !
hi, can i have the source code projot11@gmail.com
ReplyDeleteHi !! do i need to change anything in the android manifest?
ReplyDeleteEmail me please for the answer.. Thank you very much I will really appreciate it.
ReplyDeleteHere's my email. mikeeconcillado@yahoo.com.ph
Hello! Do you have any source code regarding finding the interest, principal, rate, time in android? If so, kindly email me again. Thank you very much.
ReplyDeletehello sir i tried ur code but it gives error when i tap any button on calculator pls help me to figure it out.....apurva16.sharma@gmail.com
ReplyDeleteVery, very small issue: does not have zero key. Fixing it may be a homework for the reader ;-)
ReplyDeleteCan you mail me da complete source code?
ReplyDeleteChilieyes@yahoo.com
ty
Deletehow can i add dot? Help me please
Deletekarthikkamath1911@gmail.com
ReplyDeletepls send me the code
ReplyDeletesend me the code to at
ReplyDeletersrawat1993@gmail.com
You can see the code in complete link in this blog site, http://tobidae.blogspot.com/2013/10/how-to-make-calculator-app-in-eclipse.html it is ingenius
ReplyDeletepublic void btn1Clicked(View v){
ReplyDeleteinsert(1);
not working
Thank you very much...very much appreciated.
ReplyDeleteHi,
ReplyDeleteCan you please explain how do we implement decimal functionality?
TY
same problem i tried so many times does not work
DeleteThis comment has been removed by the author.
ReplyDeletecan anyone send me the complete code on divya.vy825@gmail,com?
ReplyDeletePlease somebody send me a complete code of calculator. My email aidas.maleckas@gmail.com
ReplyDeletecould u please screen shoot the app?
ReplyDeletehi
ReplyDeletegreat and simple to understand
can you please send the entire sorce code.
Thanks
Frank
email.id-franklin.franz@gmail.com
Hi,
ReplyDeleteI'm getting a whole bunch of errors. Please send me the the complete code to miller.seke@gmail.com. Thanks- Seth
same here....send d source code plz....
ReplyDeleteblindbat.20@gmail.com
hello ...when I tried to run the program. it showed the message "Unfortunately Simple calculator has stopped"..need help please
ReplyDeletemy email is imstillstunner@googlemail.com
ReplyDeletehello ...when I tried to run the program in avd ..it showed the message "Unfortunately Simple calculator has stopped"..need help please
ReplyDeletethe application instead of running ,is getting crashed repeatedly
ReplyDeletecan u plzz send me the full length code
my email id: kmnkulkarni79@gmail.com
can u tell me the code to add . button also to calculate float value.
ReplyDeleteHy.. this application is running well.. highly recommended..
ReplyDeletebut how about sin con tan?? can we resolve it? if we want to calculate sin 90?? thankss admin
Great! Thank you for the author of this code...! GBY!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteEverything is better but mouse click event handling is the best man!
ReplyDeleteThanks a lot! Icould install and run on my nevono as well as my tab karbonn 1 !!
ReplyDeleteThanks for sharing! But there's runtime errors. Pls send me the complete code. email: doub1ea1006@gmail.com. Thanks alot!
ReplyDeleteHello, this works amazing. But what I want to do now is to show the full equation in the edittext. For example, I want to show 8 + 8 , then when I click the = sign, thats when the results change to 16. I just want to show the whole equation. How can I do that with your code?
ReplyDeleteHi..am getting this problem each time am trying to run the app :
ReplyDelete`main cannot be resolved or is not a field`
The code with the `main` is as follows :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HELP PLZ :(
my emailid is adqureshi@gmail.com
This comment has been removed by the author.
ReplyDeleteCan you send me the full source code plz,
ReplyDeletejuventus199548@gmail.com
thx.
Hi can you pls send me the source code at saurabh1832@gmail.com
ReplyDeleteplz send me source code at sardartashaf@gmail.com
ReplyDeleteThis comment has been removed by the author.
ReplyDeletehello, could you please send me full source code, encountering two errors in main.java file
ReplyDeletegaetano.1993@yahoo.com
import android.app.Activity;
Deleteimport android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public String str = "";
Character op = 'p';
int i, num, numtemp;
EditText showResult;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showResult = (EditText) findViewById(R.id.result_id);
}
public void btn1Clicked(View v) {
insert(1);
}
public void btn2Clicked(View v) {
insert(2);
}
public void btn3Clicked(View v) {
insert(3);
}
public void btn4Clicked(View v) {
insert(4);
}
public void btn5Clicked(View v) {
insert(5);
}
public void btn6Clicked(View v) {
insert(6);
}
public void btn7Clicked(View v) {
insert(7);
}
public void btn8Clicked(View v) {
insert(8);
}
public void btn9Clicked(View v) {
insert(9);
}
public void btnplusClicked(View v) {
perform();
op = '+';
}
public void btnminusClicked(View v) {
perform();
op = '-';
}
public void btndivideClicked(View v) {
perform();
op = '/';
}
public void btnmultiClicked(View v) {
perform();
op = '*';
}
public void btnequalClicked(View v) {
calculate();
}
public void btnclearClicked(View v) {
reset();
}
private void reset() {
// TODO Auto-generated method stub
str = "";
op = 'p';
num = 0;
numtemp = 0;
showResult.setText("");
}
private void insert(int j) {
// TODO Auto-generated method stub
str = str + Integer.toString(j);
num = Integer.valueOf(str).intValue();
showResult.setText(str);
}
private void perform() {
// TODO Auto-generated method stub
str = "";
numtemp = num;
}
private void calculate() {
// TODO Auto-generated method stub
if (op == '+')
num = numtemp + num;
else if (op == '-')
num = numtemp - num;
else if (op == '/')
num = numtemp / num;
else if (op == '*')
num = numtemp * num;
showResult.setText("" + num);
}
}
This comment has been removed by the author.
ReplyDeleteSir,,please send me the source code,,,i got "unfortunately....." problem,,,here is my id
ReplyDeletepasu.cs@gmail.com
sir i really need your source code in the simple calculator.. philipjanbaruis@gmail.com
ReplyDeletehave u source code for scientific calculator ?
ReplyDeleteI am not getting any errors .... but when i run the program, the layout is not being displayed on the screen ... please help ..!!
ReplyDeletehy ive tried ur code but i cant run it .. it says "Your project contains error(s) ..
ReplyDeletesir can u send me the full code please .
heres my email christoedge@gmail.com
can you plz tell me where you called the methods that are defined in your source code?
ReplyDelete1) insert() method is called when we click a number 1 to 9
Deletepublic void btn3Clicked(View v){
insert(3);
}
2) perform method is called when we click +,-,/,*
public void btnminusClicked(View v){
perform();
op = '-';
}
3) calculate method is called when we click " = "
public void btnequalClicked(View v){
calculate();
}
hello Arun
DeleteI added zero and double zero button in my app but on click of double zero button its displaying only "0" and I also want to add "." in my calculator please help me out. Ty
thanks "arun krishna"
ReplyDeleteThe calculator doesn't display the signs +,-,*,/. Why is that? Is there any extra code to be written for that signs to display?
ReplyDeleteyes you can code some lines extra for showing these charcters
DeleteAndroid Development Tutorial: Creating a Simple Basic Calculator : https://www.youtube.com/watch?v=8gE5pQNK3Wc
ReplyDeletethanks its Work if u set this Variable to to
ReplyDeletefloat i,num,numtemp; instead of int you can then find the Calculation in Decimal other wise this code does not show the result of 1/2 as well only integers values are returned thanks it is working and nice effort
Your comment is awaiting moderation.
ReplyDeletegood information but difficult for beginners i have found it simple on http://infoocode.blogspot.com/2015/11/Simple-android-calculator-code.html
can you give the full source code with "." and "%" ....
ReplyDeletemy id is : multaniidrish39@yahoo.com
Please send complete code please temifadstellah50@gmail
ReplyDeleteplease send the total code with adding DOT My Mail ID : pdvsnarayana@gmail.com
ReplyDeleteplz send me the total code to complete this
ReplyDeletemy id is irfanqasim750@gmail.com
hello Arun
ReplyDeleteI added zero and double zero button in my app but on click of double zero button its displaying only "0" and I also want to add "." in my calculator please help me out my mail id is: riteshs027@gmail.com. Ty
Hey Arun,
ReplyDeleteI'm trying to run your app but keep getting two errors.
One of them says: error: cannot find symbol variable main
I'm wondering if you can help me out with this.
Thanks
Email is ad.litterateur@gmail.com
Hi
ReplyDeleteyou have build an excellent cal just one error i am receiving is that main is not defined in R.java
how to solve that
Kind Regards
Aditya
plz send me the complete code.. i am getting same error
ReplyDeletemy id is rizwansheikh464@gmail.com
Its a good tutorial to get the basics clear you can modify the codes and add new features later on, I was able to build my own Calculator app with history save support..
ReplyDeleteApp Link : https://goo.gl/oqhLK2
in the function perform()
ReplyDeleteWhy you have set numtemp=num?
what does it will do?
what's the purpose of insert function ???
ReplyDeleteI am run it on Ubuntu 14.
ReplyDeleteCan you please resolve my following error??
[MainActivity] Unable to resolve target 'android-8'
Can u please send me the source code ?
ReplyDeleteMy email id is --- rajnishkumar307@gmail.com
i will be thankful to u
Thanks a lot.
ReplyDeleteVery good
ReplyDeletekindly send me this code on my email
ReplyDeleteknvlsh88@gmail.com is my email