This example demonstrates I’ll show you how to Write a program to create a simple calculator in Android Studio sing Kotlin.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="4dp"> <TextView android:id="@+id/textResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="70dp" android:background="#008080" android:padding="5dp" android:text="Code4Example" android:textColor="#fff" android:textSize="24sp" android:textStyle="bold" /> <EditText android:id="@+id/editNum1" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <EditText android:id="@+id/editNum2" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/editNum1" android:layout_centerInParent="true" /> <GridLayout android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:layout_below="@+id/editNum2" android:columnCount="2" android:rowCount="2" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="1dp" android:onClick="btnAdd" android:text="+" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="1dp" android:onClick="btnSub" android:text="-" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="1dp" android:onClick="btnMul" android:text="*" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="1dp" android:onClick="btnDiv" android:text="/" /> </GridLayout> </RelativeLayout> |
Step 3 − Add the following code to src/MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import android.os.Bundle import android.view.View import android.widget.EditText import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { lateinit var editNum1:EditText lateinit var editNum2:EditText lateinit var textResult: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "Code4Example.Com" editNum1 = findViewById(R.id.editNum1) editNum2 = findViewById(R.id.editNum2) textResult = findViewById(R.id.textResult) } fun btnAdd(view: View?) { val num1 = editNum1.text.toString().toDouble() val num2 = editNum2.text.toString().toDouble() val result = num1 + num2 textResult.text = java.lang.Double.toString(result) } fun btnSub(view: View?) { val num1 = editNum1.text.toString().toDouble() val num2 = editNum2.text.toString().toDouble() val result = num1 - num2 textResult.text = java.lang.Double.toString(result) } fun btnMul(view: View?) { val num1 = editNum1.text.toString().toDouble() val num2 = editNum2.text.toString().toDouble() val result = num1 * num2 textResult.text = java.lang.Double.toString(result) } fun btnDiv(view: View?) { val num1 = editNum1.text.toString().toDouble() val num2 = editNum2.text.toString().toDouble() val result = num1 / num2 textResult.text = java.lang.Double.toString(result) } } |
Output: