https://kotlinlang.org logo
#android
Title
# android
d

DJ2

12/08/2018, 11:08 PM
Quick quesiton, so
findViewById
is no longer necessary,
Copy code
val btnSaveClick= findViewById<Button>(R.id.btnSave)
      btnSaveClick?.setOnClickListener {
          Toast.makeText(this@ArticleAdapter, "Article saved.", Toast.LENGTH_LONG).show()
      }
What should be used instead. I have this in my activity.xml:
Copy code
<Button
            android:id="@+id/btnSave"
            android:layout_width="70dp"
            android:layout_height="40dp"
            android:onClick="btnSave"
            android:text="@string/save"
            android:textAlignment="center" />
t

tevjef

12/09/2018, 12:21 AM
If you have Kotlin Android Extensions applied to your project. The
findViewById
code will be generated for you. https://kotlinlang.org/docs/tutorials/android-plugin.html
Copy code
btnSave.setOnClickListener { }
You reference the view in code by the id.
d

DJ2

12/09/2018, 1:28 AM
in my
onCreate
function do I have to do this
btnSave.setOnClickListener(this);
?
t

Thiago Pereira

12/09/2018, 1:42 AM
Nops. Just button.setOnClickListener{}
d

DJ2

12/09/2018, 1:46 AM
It has to be a function right?
t

Thiago Pereira

12/09/2018, 3:39 AM
It's a block
{ Your code }
d

DJ2

12/09/2018, 4:22 AM
It’s expecting a member declaration
t

Thiago Pereira

12/09/2018, 10:41 AM
Can u show your code?
d

DJ2

12/09/2018, 8:10 PM
This is my recyclerView adapter
Copy code
import kotlinx.android.synthetic.main.articlerecycler_item.view.*


class ArticleAdapter(private val controller: IController) : RecyclerView.Adapter<ArticleViewHolder>() {

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticleViewHolder {
      val view = LayoutInflater.from(parent.context).inflate(R.layout.articlerecycler_item, parent, false)
      return ArticleViewHolder(view)


  }
    /** Called when the user taps the Save button  */
     btnSave.setOnClickListener(){
        (R.layout.articlerecycler_item)
        AlertDialog.Builder(this)
                .setMessage("Article Saved.")
                .create()
                .show()
        // Do something in response to button click
    }

  override fun getItemCount(): Int {
      return controller.articles.size
  }

  override fun onBindViewHolder(holder: ArticleViewHolder, position: Int) {

      val article: Article = controller.articles[position]

      if(article.title.length > 100) {
        holder.itemView.titleTv.text = "${article.title.substring(0, 99)}..."
      } else {
        holder.itemView.titleTv.text = article.title
      }

      holder.itemView.authorTv.text = article.author
  }
}
class ArticleViewHolder(view: View?) : RecyclerView.ViewHolder(view)
and my button in my layout xml
Copy code
<Button
            android:id="@+id/btnSave"
            android:layout_width="70dp"
            android:layout_height="40dp"
            android:onClick="btnSave"
            android:text="@string/save"
            android:textAlignment="center" />
t

Thiago Pereira

12/10/2018, 1:06 AM
Remove the ( ) from your btnSave. U can do just button.setOnClickListener { your code }