Git Product home page Git Product logo

Comments (2)

android-dev-lxl avatar android-dev-lxl commented on June 18, 2024

Hi,
Thank you for your feedback. We verified this issue in the latest stable version of the Android Studio 3.5.3.
The solution, https://github.com/google-developer-training/android-kotlin-fundamentals-apps/tree/c67904408b9281d8e366ca3c4edacb8d4c552e36/ColorMyViews is working fine without any problems.
Closing this issue for now.

from android-kotlin-fundamentals-apps.

kevinadhiguna avatar kevinadhiguna commented on June 18, 2024

Hi,
Thank you for your feedback. We verified this issue in the latest stable version of the Android Studio 3.5.3.
The solution, https://github.com/google-developer-training/android-kotlin-fundamentals-apps/tree/c67904408b9281d8e366ca3c4edacb8d4c552e36/ColorMyViews is working fine without any problems.
Closing this issue for now.

That did not work for me. This is what I did :

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)

        // Make a call to the setListeners function
        setListeners()
    }

    /**
     * Attaches listeners to all the views.
     */
    private fun setListeners() {
        // Get a reference for each TextView
        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)

        // Get a reference to Constraint Layout as defined in activity_main.xml
        val rootConstraintLayout = findViewById<View>(R.id.constraint_layout)

        // Get a reference to the buttons
        val redButton = findViewById<Button>(R.id.red_button)
        val greenButton = findViewById<Button>(R.id.green_button)
        val yellowButton = findViewById<Button>(R.id.yellow_button)

        // Make a list of views
        val clickableViews: List<View> = listOf(boxOneText, boxTwoText, boxThreeText, boxFourText, boxFiveText, rootConstraintLayout, redButton, greenButton, yellowButton)

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

    /**
     * Sets the background color of a view depending on it's resource id.
     * This is a way of using one click handler to do similar operations on a
     * group of views.
     */
    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)

            // Boxes using custom colors for background
            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)

            // Set background color to light gray when background is touched
            else -> view.setBackgroundColor(Color.LTGRAY)
        }
    }
}

I got an error saying Unresolved reference for box_three_text, box_four_text, and box_five_text. Any help is apreciated.

from android-kotlin-fundamentals-apps.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.