Rusticalo
Rusticalo

Reputation: 3

OnClickListener for multiple buttons does not work

I searched here fo a solution and on google but it doesn't work for me and I can't understand why. I did everything like in this solution: Multiple OnClickListener in Kotlin

XML Code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnP1"
        android:layout_width="60dp"
        android:layout_height="54dp"
        android:layout_marginTop="200dp"
        android:fontFamily="sans-serif-medium"
        android:shape="rectangle"
        android:text="1"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold"
        app:backgroundTint="#98BFD1"
        app:layout_constraintEnd_toStartOf="@+id/btnP2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtDart1"
        tools:text="1" />

    <Button
        android:id="@+id/btnP2"
        android:layout_width="60dp"
        android:layout_height="54dp"
        android:layout_marginTop="334dp"
        android:shape="rectangle"
        android:text="2"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold"
        app:backgroundTint="#98BFD1"
        app:layout_constraintEnd_toStartOf="@+id/btnP3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btnP1"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="2" />
        
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt:

package com.example.test

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private var toast: Toast? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnP1.setOnClickListener(this)
        btnP2.setOnClickListener(this)

    }

    override fun onClick(view: View?)
    {
        when ( view!!.id)
        {
            TODO()
        }

    }

    private fun myToast(string: String)
    {
        toast = Toast.makeText(this,string,Toast.LENGTH_SHORT)
        toast?.show()
    }
}

Problem: btnP1.setOnClickListener(this) and btnP2.setOnClickListener(this)

-> btnP1 and btnP2 are always red -> "unresolved reference"

Why does it work for everyone but not in my case?

Thank you!

Upvotes: 0

Views: 71

Answers (2)

At Build.gardle(app)

ADD

plugins {
id 'kotlin-android-extensions'
}

or

apply plugin: 'kotlin-android-extensions'

depends on your version. and then sync

After that you would be able to use your button or whatever without using findViewById

if you still don't find it. make sure to import this at your KotlinFile ***normally it will import auto after you type the id name like in your case btnP1 ***

import kotlinx.android.synthetic.main.YOUR_XML_FILE_NAME.*   (Don't forget *)

example

import kotlinx.android.synthetic.main.activity_main.*

more info https://antonioleiva.com/kotlin-android-extensions/

Upvotes: 1

Jan Undrych
Jan Undrych

Reputation: 182

What you have declared in the XML is not automatically available in your code. You have not defined or declared the btnP1 and btnP2 in your Kotlin code, and therefore compiler does not know what to refer to, hence the error

Use findViewById to obtain a reference to your buttons

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btnP1 = findViewById<Button>(R.id.btnP1)
        val btnP2 = findViewById<Button>(R.id.btnP2)

        btnP1.setOnClickListener(this)
        btnP2.setOnClickListener(this)

    }

You can read more about this method here

Upvotes: 0

Related Questions