4

I am a beginner in kotlin, trying to develop sample projects. I tried using View.OnClickListener interface to get Id of any view clicked in my layout. I have 9 buttons in my layout (tic tac toe game) but when the code is run nothing happens on clicking any buttons, its not even showing any errors.

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var binding : ActivityMainBinding
    var active = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        
    }

    override fun onClick(view: View?) {
        val button = view as Button
        button.setBackgroundColor(Color.parseColor("#ffffff"))
        if(active == 1) {
            button.text = "X"
            button.setTextColor(Color.parseColor("#FF6200EE"))
            button.textSize = 20f
            button.isEnabled = false
            active = 0
        }
        else {
            button.text = "O"
            button.setTextColor(Color.parseColor("#FF6200EE"))
            button.textSize = 20f
            button.isEnabled = false
            active = 1
        }
    }
}

xml file

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

    <TableLayout
        android:id="@+id/tlTableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <TableRow
            android:id="@+id/trRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/bnOne"
                android:layout_width="40pt"
                android:layout_height="40pt"
                android:layout_marginRight="5pt"/>

            <Button
                android:id="@+id/bnTwo"
                android:layout_width="40pt"
                android:layout_height="40pt"
                android:layout_marginRight="5pt"/>

            <Button
                android:id="@+id/bnThree"
                android:layout_width="40pt"
                android:layout_height="40pt"/>

        </TableRow>

        <TableRow
            android:id="@+id/trRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/bnFour"
                android:layout_width="40pt"
                android:layout_height="40pt"
                android:layout_marginRight="5pt"/>

            <Button
                android:id="@+id/bnFive"
                android:layout_width="40pt"
                android:layout_height="40pt"
                android:layout_marginRight="5pt"/>

            <Button
                android:id="@+id/bnSix"
                android:layout_width="40pt"
                android:layout_height="40pt"/>

        </TableRow>

        <TableRow
            android:id="@+id/trRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/bnSeven"
                android:layout_width="40pt"
                android:layout_height="40pt"
                android:layout_marginRight="5pt"/>

            <Button
                android:id="@+id/bnEight"
                android:layout_width="40pt"
                android:layout_height="40pt"
                android:layout_marginRight="5pt"/>

            <Button
                android:id="@+id/bnNine"
                android:layout_width="40pt"
                android:layout_height="40pt"/>

        </TableRow>

    </TableLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
1
  • Can you add your xml file to your question? Commented Feb 15, 2021 at 14:41

1 Answer 1

2

You didn't attached instance of buttonClickListener with Acitivty View.OnClickListener

For that you need to add line

button.setOnClickListener(this)

Below is implemantation of how to attach onClick listener with button instances

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.bnOne.setOnClickListener(this)
        binding.bnTwo.setOnClickListener(this)
        ................
        ................
        ................
        binding.bnNine.setOnClickListener(this)
        
    }
Sign up to request clarification or add additional context in comments.

4 Comments

thank you for the reply, but it was my mistake in the question. Since the above mentioned method was not working I implemented the functionality by this method. I forgot to change the code while posting. Question is now updated. The real problem is, onClick function is not invoking when a click is registered in the layout. I had done the same functionality before using Java by implementing onClickListener interface, so when ever a click is registered in the layout it will automatically call the overridden onClick functoin. But when I tried to do the same using Kotlin nothing is happening.
Thank You, but what if am adding these buttons dynamically from backend. Then how can I get the ID's of any button clicked?
OnClick after casting view into button, then button.getId() funcation will fetch and check will xml id like (button.getId() == R.id.bnOne) this will give status of id's match
Yeah got it, but the real problem am facing is, OnClick function is not invoking on any registered click. If the buttons are dynamic then I may not be able to call setOnClickListener method for all the buttons in the program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.