Issue
I'm a Swift
dev and I'm just getting into Kotlin
so I'm unfamiliar with how things work.
In Swift
if I create a button and add an action/target to it, and in that action I add a print statement
it gets printed out in the console.
lazy var myButton: UIButton = {
let button = UIButton(type: .system)
// create button
button.addTarget(self, action: #selector(myButtonPressed), for: .touchUpInside)
return button
}()
@objc func myButtonPressed() {
print("this gets printed to the console")
}
But in Kotlin
when I had a print statement
nothing gets printed to the Build Output
nor the Event Log
val myButton: Button = findViewById(R.id.myButtonId)
myButton.setOnClickListener { myButtonPressed() }
private fun myButtonPressed() {
print("nothing gets printed to the console, I have to use the Toast function")
}
I have to use
private fun myButtonPressed() {
Toast.makeText(this, "this briefly appears inside the emulator", Toast.LENGTH_SHORT).show()
}
Am I doing something wrong or is this the way it's supposed to work?
Solution
I had to add to use Log.d()
and I had to run it in Debug mode
:
import android.util.Log // *** 1. include this import statement ***
private val TAG = "MainActivity" // *** 2. add this constant, name it TAG and set the value to the name of the Activity ***
val myButton: Button = findViewById(R.id.myButtonId)
myButton.setOnClickListener { myButtonPressed() }
private fun myButtonPressed() {
Log.d(TAG, ">>>>> now this prints to the console <<<<<<") // *** 3. add the TAG inside the first param inside the Log.d statement ***
}
Answered By - Lance Samaria
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.