Calculator App

Introduction:

I hope you guys are doing absolutely well. In this content I’m gonna provide you the step by step process with source code, how to built a calculator application in Android Studio. The process is really simple and easy to understand.

Also Read –> 5 Open Source Android Apps with GitHub Links

STEP 1: Create a project in Android Studio by taking a Empty Activity as layout.

Simple Calculator developing

STEP 2: Go to the activity_main.xml file and paste the following code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" android:orientation="vertical">

<EditText
android:id="@+id/editText"
style="@style/Widget.AppCompat.Light.AutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="130dp"
android:clickable="false"
android:cursorVisible="false"
android:ems="10"
android:inputType="textPersonName"
android:textColor="@color/black"
android:textSize="21sp"
tools:style="@style/Widget.AppCompat.AutoCompleteTextView" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"

>

<Button
android:id="@+id/btnOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:text="1"
android:layout_weight="1"
android:textSize="18sp"

/>

<Button
android:id="@+id/btnTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:text="2"
android:textSize="18sp" />

<Button
android:id="@+id/btnThree"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:text="3"
android:textSize="18sp" />

<Button
android:id="@+id/btnPlus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/cyan"
android:text="+"
android:textColor="@color/white"
android:textSize="18sp" />
</LinearLayout>


<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">

<Button
android:id="@+id/btnFour"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:text="4"
android:textSize="18sp"

/>

<Button
android:id="@+id/btnFive"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:text="5"
android:textSize="18sp" />

<Button
android:id="@+id/btnSix"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:text="6"
android:textSize="18sp" />

<Button
android:id="@+id/btnMinus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/cyan"
android:text="-"
android:textColor="@color/white"
android:textSize="18sp" />
</LinearLayout>


<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
>

<Button
android:id="@+id/btnSeven"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:text="7"
android:textSize="18sp"

/>

<Button
android:id="@+id/btnEight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:text="8"
android:textSize="18sp" />

<Button
android:id="@+id/btnNine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:text="9"
android:textSize="18sp" />

<Button
android:id="@+id/btnMult"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/cyan"
android:text="x"
android:textColor="@color/white"
android:textSize="18sp" />
</LinearLayout>


<LinearLayout
android:id="@+id/linearLayout6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
>

<Button
android:id="@+id/btnPoint"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:text="."
android:textSize="18sp"

/>

<Button
android:id="@+id/btnZero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:text="0"
android:textSize="18sp" />

<Button
android:id="@+id/btnClear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:textColor="@color/red"
android:text="C"
android:textSize="18sp" />

<Button
android:id="@+id/btnDiv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/cyan"
android:text="/"
android:textColor="@color/white"

android:textSize="18sp" />
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
>


<Button
android:id="@+id/btnEqual"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:text="="
android:layout_weight="1"
android:textColor="@color/white"
android:textSize="18sp" />
<Button
android:id="@+id/btnSqur"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/cyan"
android:text="Square"
android:textColor="@color/white"
android:textSize="14sp"
/>

</LinearLayout>
</LinearLayout>

STEP 4: Go to the colors.xml file and paste the following code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3e79e6</color>
<color name="colorPrimaryDark">#1a33d1</color>
<color name="colorAccent">#FF4081</color>
<color name="black">#000000</color>
<color name="white">#FFFFFF</color>
<color name="cyan">#FF2390D9</color>
<color name="red">#FFED0F34</color>
</resources>

STEP 5: Now navigates to the MainActivity.java file and paste the following code.

Calculator App building

Also Read

Open Source Android Apps

5 Awesome Open Source Android Apps To Boost Your Development Skills What does ‘Open Source’ mean? In general, open source refers to any program whose source code is made available…

Run Your First Android App

You can run your app in two ways. One is via Android Emulator and another one is via Real Device. In Android Studio, click the app module in the Project…

Build your first android app

In the Welcome to Android Studio window, click Start a new Android Studio project.   Or if you have a project opened, select File > New Project. In the Create…

Leave a Reply

avatar
  Subscribe  
Notify of