skip to Main Content
package com.example.tipcalculator

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import kotlin.text.toFloat as kotlinTextToFloat


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        supportActionBar?.hide()
        val tenPerTip: Button =       findViewById(R.id.tenPerTipButton)
        val fifteenPerTip: Button =   findViewById(R.id.fifteenPerTipButton)
        val twentyPerTip: Button =    findViewById(R.id.twentyPerTipButton)
        val customTip: Button =       findViewById(R.id.customTipSubmit)
        val sumRaw: EditText =        findViewById(R.id.billEnter)
        val tipOnlyResult: TextView = findViewById(R.id.tipOnlyResult)
        val totalResult: TextView =   findViewById(R.id.totalResult)
        val sumString = sumRaw.toString()
        val sumInput = sumString.toInt()

        tenPerTip.setOnClickListener{
            val sumTotal = sumInput * 1.1
            val tipOnly = sumInput * 0.1
            tipOnlyResult.text = tipOnly.toString()
            totalResult.text = sumTotal.toString()
        }
        fifteenPerTip.setOnClickListener{

        }
        twentyPerTip.setOnClickListener{

        }
        customTip.setOnClickListener{

        }
    }
}

I was trying to switch the EditText input to a string and from there to a float so that I can do calculations on it. On the line with val sumInput = sumString.toInt() the code breaks. It will compile, but when I try to run an emulator it casts error codes about the toInt declaration. The code is using toInt in this because I was trying to see if the emulator would like that. Also whenever I declare that toInt it highlights in a light yellow italic font, which I haven’t seen before.

2

Answers


  1. You seem to think the that when you write val sumString = sumRaw.toString(), it gives you the text entered in R.id.billEnter which is not correct. to get text from an EditText you have to use text property. As for your code it can be fixed as

    tenPerTip.setOnClickListener{
        val sumString = sumRaw.text.toString()
        val sumInput = sumString.toInt()
        val sumTotal = sumInput * 1.1
        val tipOnly = sumInput * 0.1
        tipOnlyResult.text = tipOnly.toString()
        totalResult.text = sumTotal.toString()
    }
    
    Login or Signup to reply.
  2. I have Edited your code.
    Changes to the code is explained with the comments below.

    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            supportActionBar?.hide()
            val tenPerTip: Button = findViewById(R.id.tenPerTipButton)
            val fifteenPerTip: Button = findViewById(R.id.fifteenPerTipButton)
            val twentyPerTip: Button = findViewById(R.id.twentyPerTipButton)
            val customTip: Button = findViewById(R.id.customTipSubmit)
            val sumRaw: EditText = findViewById(R.id.billEnter)
            val tipOnlyResult: TextView = findViewById(R.id.tipOnlyResult)
            val totalResult: TextView = findViewById(R.id.totalResult)
    
            /** if you put sumString and sumInput here, what happens is when your app
             *  is created and the onCreate method is called sumString is initialized
             *  here without using .text, i.e., sumRaw.text command what happens is sumString
             *  will be initialized with sumRaw value (i guess maybe sumRaw id) and you will 
             *  get error.
             *  Also, this is only called once when all the other variable are initialized.
             *  If you want to use it outside use a TextWatcher and change the variable as
             *  soon as it is updated in the EditText.
             *  A work-around would be initializing this value inside OnClickListener, what
             *  happens here is whenever you click tenPerTip Button sumString is Initialized
             *  with current values in sumRaw EditText.
             *  
             *  Do use .text else it will give errors.
             */
            
    
            tenPerTip.setOnClickListener {
    
                val sumString = sumRaw.text.toString()
                
                // what happens here is sumString is converted to Double if it has valid pattern
                // else it will return null
                // And then the elvis operator will check for null. If its null, it will not
                // not proceed further and will get out the listener. 
                
                val sumInput = sumString.toDoubleOrNull() ?: return@setOnClickListener
    
                val sumTotal = sumInput * 1.1
                val tipOnly = sumInput * 0.1
                tipOnlyResult.text = tipOnly.toString()
                totalResult.text = sumTotal.toString()
            }
            fifteenPerTip.setOnClickListener {
    
            }
            twentyPerTip.setOnClickListener {
    
            }
            customTip.setOnClickListener {
    
            }
        }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search