Abhas Sharma
Abhas Sharma

Reputation: 101

Why it's unresolved reference error for calling setBackgroundResource() on an ' id ' in Mainactivity.kt?

package com.example.colormyviews

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setListeners()
    }


    private fun setListeners(){
        val boxOneText = findViewById<TextView>(R.id.box_one_text)
        val boxTwoText = findViewById<TextView>(R.id.box_two_text)
        val boxThreeText = findViewById<TextView>(R.id.box_three_text)
        val boxFourText = findViewById<TextView>(R.id.box_four_text)
        val boxFiveText = findViewById<TextView>(R.id.box_five_text)

        val rootConstraintLayout = findViewById<View>(R.id.info_text)

        val redButton = findViewById<Button>(R.id.red_button)
        val greenButton = findViewById<Button>(R.id.green_button)
        val yellowButton = findViewById<Button>(R.id.yellow_button)

        val clickableViews: List<View> =
                listOf(
                        boxOneText, boxTwoText, boxThreeText,
                        boxFourText, boxFiveText, rootConstraintLayout,
                        redButton, greenButton, yellowButton
                )



        for (item in clickableViews){
            item.setOnClickListener{makeColored(it)}
        }


    }

    private fun makeColored(view: View){

        when (view.id) {

            // Boxes using Color class colors for the background
            R.id.box_one_text -> view.setBackgroundColor(Color.DKGRAY)
            R.id.box_two_text -> view.setBackgroundColor(Color.GRAY)
            R.id.box_three_text -> view.setBackgroundColor(Color.BLUE)
            R.id.box_four_text -> view.setBackgroundColor(Color.MAGENTA)
            R.id.box_five_text -> view.setBackgroundColor(Color.BLUE)

            R.id.red_button -> box_three_text.setBackgroundResource(R.color.my_red)
            R.id.yellow_button -> box_four_text.setBackgroundResource(R.color.my_yellow)
            R.id.green_button -> box_five_text.setBackgroundResource(R.color.my_green)
            else -> view.setBackgroundColor(Color.LTGRAY)
        }
    }


}

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:id="@+id/constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/box_one_text"
        style="@style/whiteBox"

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="@dimen/margin_wide"
        android:layout_marginRight="16dp"
        android:text="@string/box_one"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/box_two_text"
        style="@style/whiteBox"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="@string/box_two"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/box_one_text" />

    <TextView
        android:id="@+id/box_three_text"
        style="@style/whiteBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_wide"
        android:layout_marginEnd="@dimen/margin_wide"
        android:text="@string/box_three"
        app:layout_constraintBottom_toTopOf="@+id/box_four_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/box_two_text"
        app:layout_constraintTop_toTopOf="@+id/box_two_text" />

    <TextView
        android:id="@+id/box_four_text"
        style="@style/whiteBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_wide"
        android:layout_marginTop="@dimen/margin_wide"
        android:layout_marginEnd="@dimen/margin_wide"
        android:layout_marginBottom="@dimen/margin_wide"
        android:text="@string/box_four"
        app:layout_constraintBottom_toTopOf="@+id/box_five_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/box_two_text"
        app:layout_constraintTop_toBottomOf="@+id/box_three_text" />

    <TextView
        android:id="@+id/box_five_text"
        style="@style/whiteBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_wide"
        android:layout_marginEnd="@dimen/margin_wide"
        android:text="@string/box_five"
        app:layout_constraintBottom_toBottomOf="@+id/box_two_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/box_two_text"
        app:layout_constraintTop_toBottomOf="@+id/box_four_text" />

    <TextView
        android:id="@+id/label_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:fontFamily="@font/roboto"
        android:text="@string/how_to_play"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBaseline_toBaselineOf="@+id/info_text"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/info_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:fontFamily="@font/roboto"
        android:text="@string/tap_the_boxes_and_buttons"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/label_text"
        app:layout_constraintTop_toBottomOf="@+id/box_two_text"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/red_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="@string/button_red"
        app:layout_constraintBaseline_toBaselineOf="@+id/yellow_button"
        app:layout_constraintEnd_toStartOf="@+id/yellow_button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/yellow_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:text="@string/button_yellow"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/green_button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/red_button"
        app:layout_constraintTop_toBottomOf="@+id/info_text"
        app:layout_constraintVertical_bias="1.0"/>

    <Button
        android:id="@+id/green_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:text="@string/button_green"
        app:layout_constraintBaseline_toBaselineOf="@+id/yellow_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/yellow_button" />

</androidx.constraintlayout.widget.ConstraintLayout>

In makeColored() function, while calling setBackgroundResource() on box_three_text, box_four_text, box_five_text. It throws me an ' Unresolved reference error '. However id's with same name has been declared in XML file. I can't understand where the problem lies, Please help me to understand what's going on and why it is unable to read id's in Mainactivity.kt which has been declared in Mainactivity.xm.

Upvotes: 0

Views: 870

Answers (2)

Foroogh Varmazyar
Foroogh Varmazyar

Reputation: 1585

Instead of using id you have to define global variable, like this :

class MainActivity : AppCompatActivity() {
  private  lateinit var boxOneText: View
  private lateinit var boxTwoText: View
  private lateinit var boxThreeText: View
  private lateinit var boxFourText: View
  private lateinit var boxFiveText: View

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

        setListeners()
    }

    private fun makeColored(view: View) {
        when (view.id) {
            R.id.box_one_text -> view.setBackgroundColor(Color.DKGRAY)
            R.id.box_two_text -> view.setBackgroundColor(Color.GRAY)
            R.id.box_three_text -> view.setBackgroundColor(Color.BLUE)
            R.id.box_four_text -> view.setBackgroundColor(Color.MAGENTA)
            R.id.box_five_text -> view.setBackgroundColor(Color.BLUE)

            R.id.red_button -> boxThreeText.setBackgroundResource(R.color.my_red)
            R.id.yellow_button -> boxFourText.setBackgroundResource(R.color.my_yellow)
            R.id.green_button -> boxFiveText.setBackgroundResource(R.color.my_green)
            else -> view.setBackgroundColor(Color.LTGRAY)
        }
    }

    private fun setListeners() {
        boxOneText = findViewById<TextView>(R.id.box_one_text)
        boxTwoText = findViewById<TextView>(R.id.box_two_text)
        boxThreeText = findViewById<TextView>(R.id.box_three_text)
        boxFourText = findViewById<TextView>(R.id.box_four_text)
        boxFiveText = findViewById<TextView>(R.id.box_five_text)

        val rootConstraintLayout = findViewById<View>(R.id.constraint_layout)

        val redButton = findViewById<Button>(R.id.red_button)
        val greenButton = findViewById<Button>(R.id.green_button)
        val yellowButton = findViewById<Button>(R.id.yellow_button)

        val clickableViews: List<View> =
            listOf(
                boxOneText, boxTwoText, boxThreeText,
                boxFourText, boxFiveText, rootConstraintLayout,
                redButton, greenButton, yellowButton
            )

        for (item in clickableViews) {
            item.setOnClickListener { makeColored(it) }
        }
    }
}

Upvotes: 0

Kunu
Kunu

Reputation: 5134

instead of id use reference

 R.id.red_button -> boxThreeText.setBackgroundResource(R.color.my_red)
 R.id.yellow_button -> boxFourText.setBackgroundResource(R.color.my_yellow)
 R.id.green_button -> boxFiveText.setBackgroundResource(R.color.my_green)

and make these buttons global variable. You can directly use ids if you are using kotlin synthetic, however I can't see any such import in your code. (And anyway you should not be using be kotlin synthetic as Kotlin-extension plugin is deprecated)

Upvotes: 1

Related Questions