0

fun SubmitOrder(view: View) {

    /* pricing of coffee */
    val total = quantity * 5
    val s: String = ("$$total.00")
    money.text = ("Total : $s\nThank You!").toString()

    //This is calling On click listener
    Toast.makeText(this, "order_Submitted", Toast.LENGTH_SHORT).show()
}

In this code, I need a new line before Thank You! in money.text but I am not getting any new line I am new in android development so, am not able to point out the mistake.

1 Answers1

0

Let's go thru your code line by line:


val s: String = ("$$total.00")
  • s is a bad variable name, as it's not descriptive at all
  • you don't need the (braces)
  • the :String here is optional. In such an obvious case i would emit it.
  • A $ is a sign to the kotlin compiler to include the following variable. Therefore, you can't use the $ when you mean "US-Dollar". See this post on how to escape it
  • While ".00" works, it's no good style. I suggest you use string formatting as described here.
  • can be written as val s = "\$ ${String.format("%.2f", total)}"
  • you should wherever possible use string resources, but thats out of the scope of this answer

money.text = ("Total : $s\nThank You!").toString()
  • this is correct, but unnecessary verbose:
  • "Total : $s\nThank You!" is already a string, so there's no need to .toString()
  • braces are not needed
  • can be written as money.text = "Total : $s\nThank You!"
m.reiter
  • 1,796
  • 2
  • 11
  • 31