0

I started learning to develop Android applications in Kotlin two weeks ago. I'm currently trying to make some changes to my tip calculator application and am trying to display the text field input into a totalCost TextView (where you see the "Cost of Service" text below the "Tip Amount: $20.00"), but I've no idea how to go about doing that.

Click here to see the app layout

And here's the code:

File activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/icon_cost_of_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:importantForAccessibility="no"
        app:srcCompat="@drawable/ic_store"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/cost_of_service"
        app:layout_constraintBottom_toBottomOf="@id/cost_of_service"/>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/cost_of_service"
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:hint="@string/cost_of_service"
        android:layout_marginStart="16dp"
        app:layout_constraintStart_toEndOf="@id/icon_cost_of_service"
        app:layout_constraintTop_toTopOf="parent" >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/cost_of_service_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"/>
    </com.google.android.material.textfield.TextInputLayout>

    <ImageView
        android:id="@+id/icon_service_question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:importantForAccessibility="no"
        app:srcCompat="@drawable/ic_service"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/service_question"
        app:layout_constraintBottom_toBottomOf="@id/service_question"/>

    <TextView
        android:id="@+id/service_question"
        style="@style/Widget.TipTime.TextView"
        android:layout_marginTop="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/how_was_the_service"
        app:layout_constraintStart_toStartOf="@id/cost_of_service"
        app:layout_constraintTop_toBottomOf="@id/cost_of_service" />

    <RadioGroup
        android:id="@+id/tip_options"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checkedButton="@id/option_twenty_percent"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="@id/service_question"
        app:layout_constraintTop_toBottomOf="@id/service_question">

        <RadioButton
            android:id="@+id/option_twenty_percent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/amazing_service" />

        <RadioButton
            android:id="@+id/option_eighteen_percent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/good_service" />

        <RadioButton
            android:id="@+id/option_fifteen_percent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ok_service" />
    </RadioGroup>

    <ImageView
        android:id="@+id/icon_round_up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:importantForAccessibility="no"
        app:srcCompat="@drawable/ic_round_up"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/round_up_switch"
        app:layout_constraintBottom_toBottomOf="@id/round_up_switch"/>


    <com.google.android.material.switchmaterial.SwitchMaterial
        android:id="@+id/round_up_switch"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/round_up_tip"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginStart="16dp"
        app:layout_constraintStart_toEndOf="@id/icon_round_up"
        app:layout_constraintTop_toBottomOf="@id/tip_options" />

    <Button
        android:id="@+id/calculate_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/calculate"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@id/round_up_switch"
        app:layout_constraintTop_toBottomOf="@id/round_up_switch" />

    <TextView
        android:id="@+id/tip_result"
        style="@style/Widget.TipTime.TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/calculate_button"
        tools:text="Tip Amount: $10" />

</androidx.constraintlayout.widget.ConstraintLayout>

File MainActivity.kt

package com.example.tiptime

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.tiptime.databinding.ActivityMainBinding
import java.text.NumberFormat

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.calculateButton.setOnClickListener { calculateTip() }
    }

    fun calculateTip() {
        val stringInTextField = binding.costOfServiceEditText.text.toString()
        val cost = stringInTextField.toDoubleOrNull()
        if (cost == null) {
            binding.tipResult.text = ""
            return
        }

        val tipPercentage = when (binding.tipOptions.checkedRadioButtonId) {
            R.id.option_twenty_percent -> 0.20
            R.id.option_eighteen_percent -> 0.18
            else -> 0.15
        }

        var tip = tipPercentage * cost
        if (binding.roundUpSwitch.isChecked) {
            tip = kotlin.math.ceil(tip)
        }

        val formattedTip = NumberFormat.getCurrencyInstance().format(tip)
        binding.tipResult.text = getString(R.string.tip_amount, formattedTip)
    }

}
2
  • You've already done the hard work in calculateTip! Just need to add the tip to the cost and display that amount in another TextView eh Apr 2, 2021 at 15:28
  • That's the hard part XD Apr 16, 2021 at 23:43

1 Answer 1

1

To get text from edit text box you can do the following:

    val editText = findViewById<EditText>(R.id.cost_of_service_edit_text)
    val textValue = text.text.toString()

And to set text to a textView you would do:

    val textView = findViewById<EditText>(R.id.textViewId)
    textView.text = textValue
6
  • Thanks! I added the following code: val editText = findViewById< EditText >( R.id.cost_of_service_edit_text ) val textValue = editText.text.toString() val textView = findViewById<EditText>( R.id.cost_of_service_edit_text ) textView.text = textValue There was a "Type mismatch: inferred type is String but Editable! was expected" for the textView when I ran it. Am I missing something? Apr 4, 2021 at 18:35
  • 1
    Try changing textView.text = textValue to textView.setText(textValue) Does this work?
    – Amy
    Apr 4, 2021 at 23:05
  • I replaced the code as suggested, I run the program in an Android Virtual Device (AVD), and there are no errors. Great! but the TextView isn't showing up in the app (not sure if this is a problem with the activity_main.xml or strings.xml?) Apr 5, 2021 at 15:06
  • Hmm.. Not sure if I can help with that one, as I would need more details, but If you are really stuck then you can ask another question, and I'll try and answer it :) Happy Coding!
    – Amy
    Apr 6, 2021 at 1:49
  • 1
    I got it to work. Thanks, your help was invaluable! Apr 16, 2021 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.