I can't get my app to work!

Hello everyone! Let me tell you that I’m a total newbie, but totally excited about Kotlin and it’s my hobby.

I am trying to design an application and I am not succeeding, could someone guide me?

It is an APP to divide the expenses between friends.

Here I leave the activity_main.xml and the MainActivity.kt and a picture of the UI.

Thank you in advance for any help you can give me!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
    android:id="@+id/textViewExpenditure"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Total Expenditure:" />

<EditText
    android:id="@+id/editTextExpenditure"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:hint="Enter total expenditure" />

<TextView
    android:id="@+id/textViewPersons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Number of Persons:" />

<EditText
    android:id="@+id/editTextPersons"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:hint="Enter number of persons" />

<TextView
    android:id="@+id/textViewNamePerson1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Person 1 Name:" />

<EditText
    android:id="@+id/editTextNamePerson1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter person 1 name" />

<TextView
    android:id="@+id/textViewContribution"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Contribution:" />

<EditText
    android:id="@+id/editTextContribution"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:hint="Enter contribution amount" />

<Button
    android:id="@+id/buttonCalculate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Calculate" />

<TextView
    android:id="@+id/textViewPayment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Payment: " />

<TextView
    android:id="@+id/textViewOwe"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Owe: " />

<TableLayout
    android:id="@+id/tableLayoutPeople"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:stretchColumns="*">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Contribution" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Owe" />
    </TableRow>

</TableLayout>

</LinearLayout>

package com.luiggiapp.prueba01

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TableLayout
import android.widget.TableRow
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    private lateinit var editTextExpenditure: EditText
    private lateinit var editTextPersons: EditText
    private lateinit var editTextNamePerson1: EditText
    private lateinit var editTextContribution: EditText
    private lateinit var buttonCalculate: Button
    private lateinit var textViewPayment: TextView
    private lateinit var textViewOwe: TextView
    private lateinit var tableLayout: TableLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        editTextExpenditure = findViewById(R.id.editTextExpenditure)
        editTextPersons = findViewById(R.id.editTextPersons)
        editTextNamePerson1 = findViewById(R.id.editTextNamePerson1)
        editTextContribution = findViewById(R.id.editTextContribution)
        buttonCalculate = findViewById(R.id.buttonCalculate)
        textViewPayment = findViewById(R.id.textViewPayment)
        textViewOwe = findViewById(R.id.textViewOwe)
        tableLayout = findViewById(R.id.tableLayoutPeople)

        buttonCalculate.setOnClickListener {
            calculateExpenses()
        }
    }

    private fun calculateExpenses() {
        val expenditure = editTextExpenditure.text.toString().toDouble()
        val persons = editTextPersons.text.toString().toInt()
        val contribution = editTextContribution.text.toString().toDouble()

        val payment = expenditure / persons
        val owe = payment - contribution

        textViewPayment.text = "Payment: $payment"
        textViewOwe.text = "Owe: $owe"

        // Limpiar los campos de entrada
        editTextNamePerson1.text.clear()
        editTextContribution.text.clear()

        // Crear una nueva fila para la tabla
        val row = TableRow(this)

        // Crear celdas para cada columna
        val nameCell = TextView(this)
        nameCell.text = editTextNamePerson1.text.toString()
        row.addView(nameCell)

        val contributionCell = TextView(this)
        contributionCell.text = editTextContribution.text.toString()
        row.addView(contributionCell)

        val oweCell = TextView(this)
        oweCell.text = owe.toString()
        row.addView(oweCell)

        // Agregar fila a la tabla
        tableLayout.addView(row)
    }
}

I am not sure if you can get a response here but this forum is for Kotlin specific. Maybe you can try posting this on Stack Overflow.

2 Likes