Welcome to the comprehensive documentation for the Simple Calculator App. In this guide, you will find detailed explanations of the app's features, code structure, and usage.
- Introduction
- Features
- Getting Started
- Prerequisites
- Installation
- App Structure
- XML Layout (
activity_main.xml
) - MainActivity Kotlin Code (
MainActivity.kt
) - Resources (
strings.xml
andstyles.xml
)
- XML Layout (
- Dependencies
- Usage
- EvaluateExpression Function
- Conclusion
The Simple Calculator App is a beginner-level Android application built using Kotlin and the Android Jetpack components. It offers users a user-friendly interface to perform basic arithmetic calculations and understand the fundamentals of Android app development.
- Arithmetic Operations: Addition, subtraction, multiplication, and division.
- Decimal Support: Input and calculation of decimal numbers.
- Clear Functionality: Clear input and start over (AC).
- Delete Input: Remove the last character entered (DEL).
- Parentheses Handling: Incorporate parentheses in calculations.
- Display Expression and Result: Visual representation of the input expression and calculated result.
- Expression Evaluation: Exp4j library to evaluate mathematical expressions.
- Android Studio: Ensure you have Android Studio installed and configured.
- Clone the Project: Clone or download the project from the repository.
- Open in Android Studio: Launch Android Studio and open the project directory.
- Build and Run: Build and run the app on an emulator or physical device.
The app's user interface is described using XML in the activity_main.xml
layout file. This file incorporates a ConstraintLayout that organizes the buttons and TextViews.
The MainActivity.kt
file contains the Kotlin code responsible for powering the app's functionality. This code interacts with the UI elements defined in the XML layout.
Let's explore the code in detail:
// Import necessary libraries and modules
import android.os.Bundle
import android.text.TextUtils
import android.text.method.ScrollingMovementMethod
import androidx.appcompat.app.AppCompatActivity
import com.example.simplecalculator.databinding.ActivityMainBinding
import net.objecthunter.exp4j.ExpressionBuilder
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var input: String = ""
private var bracketsCount = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// Initialize UI elements and setup
// ...
// Set up button click listeners
binding.zero.setOnClickListener { /* Logic for button zero */ }
binding.one.setOnClickListener { /* Logic for button one */ }
// ... (Repeat for other buttons)
// Handle equal button click
binding.equal.setOnClickListener {
// Evaluate the expression and display the result
if (input.isNotEmpty()) {
try {
val result = evaluateExpression(input)
binding.answerView.text = "= $result"
} catch (e: Exception) {
binding.answerView.text = "= Error"
}
}
}
}
// Function to evaluate mathematical expression using Exp4j library
private fun evaluateExpression(expression: String): String {
return try {
val exp = ExpressionBuilder(expression).build()
val result = exp.evaluate()
result.toString()
} catch (e: Exception) {
throw e
}
}
}
The strings.xml
file contains string resources used throughout the app, such as button labels and default values. The styles.xml
file defines the app's theme and appearance.
The app utilizes various libraries and dependencies to achieve its functionality. These dependencies include:
- Kotlin Standard Library: Core Kotlin functionality.
- Exp4j: A library for evaluating mathematical expressions.
- AndroidX Libraries: Core Android components and UI elements.
- Material Design Components: UI components following Material Design guidelines.
- Launch the app on your Android device.
- Use the number buttons (0-9) to input numbers.
- Use the operator buttons (+, -, *, /) to perform operations.
- Press the DOT button to add decimal points.
- Use the BRACKETS button to include parentheses in expressions.
- Press the AC button to clear the input expression.
- Press the DEL button to remove the last character.
- Press the EQUAL button to calculate the result.
- The input expression and result are displayed on the screen.
The evaluateExpression
function plays a crucial role in the app's functionality. Let's delve into how it works:
private fun evaluateExpression(expression: String): String {
return try {
val exp = ExpressionBuilder(expression).build()
val result = exp.evaluate()
result.toString()
} catch (e: Exception) {
throw e
}
}
This function takes an input expression as a parameter, uses the ExpressionBuilder from the Exp4j library to create an expression object, and then evaluates the expression using the evaluate
method. The result is converted to a string and returned. If any exceptions occur during the evaluation, they are propagated as-is.
Congratulations! You've now explored the detailed documentation of the Simple Calculator App. This app is an excellent starting point for those interested in learning about Android app development using Kotlin. Feel free to modify and enhance the app to suit your needs, and use it as a foundation for more advanced projects. Happy coding!