Step by step beginners tutorial - how to make an android calculator using android studio

Step by step beginners tutorial - how to make an android calculator using android studio

Step by step beginners tutorial - how to make an android calculator using android studio

Step by step beginners tutorial - how to make an android calculator using android studio


In the second tutorial of Android Studio tutorials (you can check them here : Android Studio Tutorials) and the programming ones (you can check them here : Programming Tutorials) we're showing you how to make a simple android calculator that can do basic calculations such as addition, subtracting, multiplication and division. I have tried to explain as much as possible that is why the video is +30minutes long and i kinda got tired in the middle of it so haha ! i am sorry for that

before watching this tutorial you need to download and install android studio ( that's an article that shows how : Download And Install Android Studio, and that is a YouTube video how to download and install android studio)



A video to explain the code : 



The used code :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/appbg"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
tools:ignore="MissingConstraints">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="the Result : "
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/result_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:textColor="@color/black"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="@+id/first_num_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:hint="first number"
android:inputType="numberDecimal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
tools:ignore="MissingConstraints">
<Button
android:id="@+id/addbtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:backgroundTint="@color/appbg2"
android:text="+"
android:textSize="20sp" />
<Button
android:id="@+id/minbtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:backgroundTint="@color/appbg2"
android:text="-"
android:textSize="20sp" />
<Button
android:id="@+id/multbtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:backgroundTint="@color/appbg2"
android:text="×"
android:textSize="20sp" />
<Button
android:id="@+id/divbtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="4dp"
android:layout_weight="1"
android:backgroundTint="@color/appbg2"
android:text="/"
android:textSize="20sp" />
</LinearLayout>
<EditText
android:id="@+id/second_num_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="32dp"
android:gravity="center"
android:hint="second number"
android:inputType="numberDecimal" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
package techituptoday.simple.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button addbtn,minbtn,multbtn,divbtn;
EditText firstNumET,secondNumET;
double FirstNum,SecondNum,Result;
char op;
TextView resultTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addbtn = findViewById(R.id.addbtn);
minbtn = findViewById(R.id.minbtn);
multbtn = findViewById(R.id.multbtn);
divbtn = findViewById(R.id.divbtn);
firstNumET = findViewById(R.id.first_num_et);
secondNumET = findViewById(R.id.second_num_et);
resultTv = findViewById(R.id.result_tv);
addbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
op='+';
try {
FirstNum= Double.parseDouble(firstNumET.getText().toString());
SecondNum= Double.parseDouble(secondNumET.getText().toString());
}catch (NullPointerException n){
Toast.makeText(MainActivity.this, "please write number", Toast.LENGTH_SHORT).show();
}catch (Exception e){
Toast.makeText(MainActivity.this, "please write valid numbers", Toast.LENGTH_SHORT).show();
}
Result = calculate(FirstNum,SecondNum,op);
resultTv.setText(String.valueOf(Result));
}
});
minbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
op='-';
try {
FirstNum= Double.parseDouble(firstNumET.getText().toString());
SecondNum= Double.parseDouble(secondNumET.getText().toString());
}catch (NullPointerException n){
Toast.makeText(MainActivity.this, "please write number", Toast.LENGTH_SHORT).show();
}catch (Exception e){
Toast.makeText(MainActivity.this, "please write valid numbers", Toast.LENGTH_SHORT).show();
}
Result = calculate(FirstNum,SecondNum,op);
resultTv.setText(String.valueOf(Result));
}
});
multbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
op='*';
try {
FirstNum= Double.parseDouble(firstNumET.getText().toString());
SecondNum= Double.parseDouble(secondNumET.getText().toString());
}catch (NullPointerException n){
Toast.makeText(MainActivity.this, "please write number", Toast.LENGTH_SHORT).show();
}catch (Exception e){
Toast.makeText(MainActivity.this, "please write valid numbers", Toast.LENGTH_SHORT).show();
}
Result = calculate(FirstNum,SecondNum,op);
resultTv.setText(String.valueOf(Result));
}
});
divbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
op='/';
try {
FirstNum= Double.parseDouble(firstNumET.getText().toString());
SecondNum= Double.parseDouble(secondNumET.getText().toString());
}catch (NullPointerException n){
Toast.makeText(MainActivity.this, "please write number", Toast.LENGTH_SHORT).show();
}catch (Exception e){
Toast.makeText(MainActivity.this, "please write valid numbers", Toast.LENGTH_SHORT).show();
}
Result = calculate(FirstNum,SecondNum,op);
resultTv.setText(String.valueOf(Result));
}
});
}
public double calculate(double firstnum,double secondnum,char o){
double res=0;
switch (o){
case '+':
res = firstnum+secondnum;
break;
case '-':
res = firstnum-secondnum;
break;
case '*':
res = firstnum*secondnum;
break;
case '/':
res = firstnum/secondnum;
break;
}
return res;
}
}