Introduction
In this article, we are going to learn how to make a simple calculator using kotlin in android. We are going to perform some basic operations such as addition, subtraction, multiplication, division, etc.
Let's start with implementation,
Step 1
Create a new project and select an empty activity.
Select kotlin language and click on the finish button.
Step 2
Go to activity_main.xml and add the following code.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="280dp"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:elevation="20dp"
app:strokeColor="@color/teal_700"
app:strokeWidth="1dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/input1"
android:inputType="number"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter 1st Number"
android:textSize="20dp" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:elevation="20dp"
app:strokeColor="@color/teal_700"
app:strokeWidth="1dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/input2"
android:inputType="number"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter 2nd Number"
android:textSize="20dp" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:elevation="20dp"
app:strokeColor="@color/teal_700"
app:strokeWidth="1dp">
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text="Result"
android:textSize="20sp"/>
</com.google.android.material.card.MaterialCardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:backgroundTint="@color/teal_700"
android:text="+"
android:textSize="20dp"
app:strokeColor="@color/white"
app:strokeWidth="2dp" />
<com.google.android.material.button.MaterialButton
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:backgroundTint="@color/teal_700"
android:text="-"
android:textSize="20dp"
app:strokeColor="@color/white"
app:strokeWidth="2dp" />
<com.google.android.material.button.MaterialButton
android:id="@+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:backgroundTint="@color/teal_700"
android:text="*"
android:textSize="20dp"
app:strokeColor="@color/white"
app:strokeWidth="2dp" />
<com.google.android.material.button.MaterialButton
android:id="@+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:backgroundTint="@color/teal_700"
android:text="/"
android:textSize="20dp"
app:strokeColor="@color/white"
app:strokeWidth="2dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Join the conversation! Your thoughts help the community grow.