MainActivity.kt - batooldshilleh/TrainingTasks GitHub Wiki

Introduction:

This is the Kotlin part. image

What can we do within this file?

  1. Package Declaration:

    • package com.example.myapplication: Declares the package name for the application.
  2. Imports:

    • Import necessary classes from Android framework: AppCompatActivity, Bundle, View, Button, EditText, TextView.
  3. MainActivity Class Declaration:

    • Declares a class named MainActivity which extends AppCompatActivity and implements View.OnClickListener interface.
  4. Properties Declaration:

    • Private properties for various UI components:
      • btnAdd, btnSub, btnMultiply, btnDivision: Buttons for arithmetic operations.
      • etA, etB: EditText fields for inputting numbers.
      • resultTv: TextView for displaying the result.
  5. onCreate() Method:

    • Override the onCreate() method from AppCompatActivity.
    • Call the superclass's onCreate() method and set the content view to the layout defined in activity_main.xml.
    • Initialize UI components by finding their views using findViewById().
    • Set OnClickListener to each button.
  6. onClick() Method:

    • Override the onClick() method from View.OnClickListener.
    • Handle click events of buttons.
    • Retrieve the input values from etA and etB, convert them to double.
    • Perform the corresponding arithmetic operation based on the clicked button using a when expression.
    • Update the resultTv TextView with the computed result.