I'm starting out with Kotlin and seems that onClick does not trigger if I implement it on an Activity Class
class MainActivity : AppCompatActivity(), View.OnClickListener{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tvTitle.text = "Hi There"
// tvTitle.setOnClickListener { this } // NOT WORKING??
tvTitle.setOnClickListener { doSomething() }
}
override fun onClick(v: View) {
Log.d("click", "Hello")
}
fun doSomething(){
Log.d("do", "Something")
}
}
I'm using Android Studio 3.0 with kotlin_version of 1.1.51, thank you in advance
Using an OnClickListener There are two ways to do this event handler programmatically : Implementing View. OnClickListener in your Activity or fragment. Creating new anonymous View.
In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.
Try this
class MainActivity : AppCompatActivity(), View.OnClickListener{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tvTitle.text = "Hi There"
tvTitle.setOnClickListener(this)
// tvTitle.setOnClickListener { doSomething() }
}
override fun onClick(v: View) {
Log.d("click", "Hello")
}
fun doSomething(){
Log.d("do", "Something")
}
}
This is my kotlin code :
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
class Activity_T : AppCompatActivity(), View.OnClickListener {
private var linear_exit: LinearLayout? = null
private var linear_history: LinearLayout? = null
private var linear_recipe: LinearLayout? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initView()
setOnClicks()
}
private fun setOnClicks() {
linear_exit!!.setOnClickListener(this)
linear_history!!.setOnClickListener(this)
linear_recipe!!.setOnClickListener(this)
}
private fun initView() {
linear_exit = findViewById(R.id.linear_exit)
linear_history = findViewById(R.id.linear_history)
linear_recipe = findViewById(R.id.linear_recipe)
}
override fun onClick(v: View) {
val item_id = v.id
when (item_id) {
R.id.linear_recipe -> Toast.makeText(this, "recipe", Toast.LENGTH_SHORT).show()
R.id.linear_history -> Toast.makeText(this, "history", Toast.LENGTH_SHORT).show()
R.id.linear_exit -> Toast.makeText(this, "exit", Toast.LENGTH_SHORT).show()
}
}
}
And this is part of my xml layout that had onclicks:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linear_exit"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/border_green"
android:gravity="center">
<net.kibotu.heartrateometer.utils.MyTextView
android:textSize="13sp"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/exit" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear_history"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/border_green"
android:gravity="center">
<net.kibotu.heartrateometer.utils.MyTextView
android:textSize="13sp"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/history" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear_recipe"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/border_green"
android:gravity="center">
<net.kibotu.heartrateometer.utils.MyTextView
android:textSize="13sp"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/recipe" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With