Android - How to Achieve Setonclicklistener in Kotlin

How to set an onClickListener to a button in kotlin

This error

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.folioreader.android.sample/com.folioreader.ui.activity.FolioActivity}:

Says that your activity is not loaded correctly, do you have something like this?

class FolioActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.folio_activity) <-- this one
}
}

Once you have set the content view you will can use your setOnClickListener as before.

 buttonBookmarks.setOnClickListener {
Toast.makeText(this, "Works", LENGTH_LONG).show()
}

Kotlin how to achieve android setOnClickListener like syntax

This is not possible if you are using Kotlin only. You can achieve this by using @FunctionalInterface of JAVA. No matter how inconsistent it seems, its by design in language.

It seems that SAM conversion only works for Java interfaces. According to the forum discussion this is by design: Kotlin has functional types and everyone should just use them. While the argument is perfectly reasonable the restriction does feel a bit inconsistent.

Please check this link

Unable to call SetOnClickListener

as I can see you cant find the button with the id. You have to options

1st option: go to ur code and use the line

val myButton: Button = findViewById(R.id.button)
myButton.setOnClickListener{
...
}

2nd option: go to gradle.app file and add into the plugins the line:

id 'kotlin-android-extensions'

and then keep the same code as you have

Android Kotlin: setOnClickListener with Intent under Fragment

This should work

US.setOnClickListener {
var intent = Intent(applicationContext, US_Fragment::class.java)
startActivity(intent)
}

Start activity from button.setOnClickListener which lies in my RecyclerView

You can use the context like this :

class Adapter(private val context: Context, private val myList: List<String>) :
RecyclerView.Adapter<Adapter.ViewHolder>()

And pass the context where required like this

    private fun confirmdel() {
startActivity(Intent(context, DelComplete::class.java))}

button.setOnClickListener is crashing my app. Kotlin 1.3.72

Through the help of this post and more research I found a solution.
I knew my problem was the creating/calling of the button. My button was not located in my main activity xml file but in another. So in my adapter is where I had to put the code.

package com.dispatch.tripsheet

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.table_list_item.view.*

class TableViewAdapter(private val movieList: List<MovieModel>) : RecyclerView.Adapter<TableViewAdapter.RowViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RowViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.table_list_item, parent, false)
return RowViewHolder(itemView)

}

private fun setHeaderBg(view: View) {
view.setBackgroundResource(R.drawable.table_header_cell_bg)
}

private fun setContentBg(view: View) {
view.setBackgroundResource(R.drawable.table_content_cell_bg)
}

override fun onBindViewHolder(holder: RowViewHolder, position: Int) {
val rowPos = holder.adapterPosition

if (rowPos == 0) {
// Header Cells. Main Headings appear here
holder.itemView.apply {
setHeaderBg(txtWOrder)
setHeaderBg(txtDElNote)
setHeaderBg(txtCompany)
// setHeaderBg(txtAddress)
setHeaderBg(txtWeight)
setHeaderBg(txtbutton1)
setHeaderBg(txtbutton2)
setHeaderBg(txttvdone)

txtWOrder.text = "WOrder"
txtDElNote.text = "DElNote"
txtCompany.text = "Company"
// txtAddress.text = "Address"
txtWeight.text = "Weight"
txtbutton1.text = "Delivered"
txtbutton2.text = "Exception"
txttvdone.text = ""
}
} else {
val modal = movieList[rowPos - 1]

holder.itemView.apply {
setContentBg(txtWOrder)
setContentBg(txtDElNote)
setContentBg(txtCompany)
// setContentBg(txtAddress)
setContentBg(txtWeight)
setContentBg(txtbutton1)
setContentBg(txtbutton2)
setContentBg(txttvdone)

txtWOrder.text = modal.WOrder.toString()
txtDElNote.text = modal.DElNote.toString()
txtCompany.text = modal.Company.toString()
// txtAddress.text = modal.Address.toString()
txtWeight.text = modal.Weight.toString()
txtbutton1.text = modal.Button1.toString()
txtbutton2.text = modal.Button2.toString()
txttvdone.text = modal.tvdone.toString()
}
}

holder.apply {
txtbutton1.setOnClickListener {
Log.e("Clicked", "Successful delivery")
txttvdone.setBackgroundResource(R.color.green)
}
txtbutton2.setOnClickListener {
Log.e("Clicked", "Exception on delivery")
txttvdone.setBackgroundResource(R.color.orange)
}
}

}

override fun getItemCount(): Int { return movieList.size + 1 // one more to add header row
}

class RowViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){

val txttvdone:TextView = itemView.findViewById<TextView>(R.id.txttvdone)
val txtbutton1:Button = itemView.findViewById<Button>(R.id.txtbutton1)
val txtbutton2:Button = itemView.findViewById<Button>(R.id.txtbutton2)
}

class MyViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
var txtbutton1 = view.findViewById<Button>(R.id.txtbutton1)
val txtbutton2:Button = itemView.findViewById<Button>(R.id.txtbutton2)
var txttvdone = view.findViewById<TextView>(R.id.txttvdone)
}
}


Related Topics



Leave a reply



Submit