Reputation: 3
Im beginner with Kotlin / Java. I've tried to make a mobile app where the idea is simple: one button that prints current time to a text table and every press of a button prints new line.
I managed to make the button to print the current line, but when I press the button again it overwrites the time printed earlier.
Currently my code looks like this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val CryButton = findViewById<Button>(R.id.CryButton)
val CryTable = findViewById<TextView>(R.id.CryTable)
CryButton.setOnClickListener {
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("hh-mm")
val formatted = current.format(formatter)
CryTable.text = formatted.toString()
I have no idea how to make the code remember the last printed text and I can't find any solutions on the web.
Upvotes: 0
Views: 2017
Reputation: 74
Please try with take old time in one variable and when you come to print new time, concat new time with old time with \n, It will work.
Upvotes: 0
Reputation: 2577
What you need to change is instead of just replacing the text of the TextView you have to add a new line to it.
The solution to do this is to replace this line CryTable.text = formatted.toString()
with this line CryTable.text = CryTable.text + "\n" + formatted.toString()
This will make it that your text view keeps the old text, add a new line to it, and then adds the new text.
Hope this helps.
Upvotes: 2
Reputation: 1066
Use append -
val CryButton = findViewById<Button>(R.id.CryButton)
val CryTable = findViewById<TextView>(R.id.CryTable)
CryTable.text = ""
CryButton.setOnClickListener {
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("hh-mm")
val formatted = current.format(formatter)
CryTable.append(formatted.toString())
Upvotes: 0