Android Studio – Setting visibility of ImageView in button callback not working

First off – I’m very new to app-coding, kotlin, xmls, etc. (Started teaching myself last week).
Having said that: I’m trying to write an activity in which an image is shown, and the user needs to press the corresponding button. (Specifically the image is of a g-clef note, and the buttons are just a list of note names, in Hebrew).
After pressing the button, I collect “correct or not” statistics, and show a “v” or “x” image so the user knows if they got it right, and then a new image is generated, until I’ve cycled through all images in the list of images to be learned.
The way I’ve got it set up, is that when a new image is generated, I redefine the button callbacks so that the correct button shows the “v” (and does a +1 to the correct answer stats, and moves on to the next image), and all the rest of the buttons show the “x” (and do a +1 to the incorrect stats, and don’t move on to the next image yet).

The problem I’m facing is that everything works except for showing the v/x image.
Stats are correctly collected, I checked. Adding a short “sleep” doesn’t help.
What am I doing wrong??

Here’s the code for what it’s worth. I’ve marked by comment the two lines that don’t do what I think they should, with the words “THIS IS NOT SHOWING”.

BTW, I’m sure there’re a ton of improvements to be made in this code, but as a beginner I’d appreciate focusing on the problem at hand and not on the plethora of general problems in my code, style, etc. 🙂

package com.example.test

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.HorizontalScrollView
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class GuessNotesGame : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_guess_notes_game)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)

            val buttonschoicecontainer = findViewById<HorizontalScrollView>(R.id.scrollviewer)
            val linearbuttons = findViewById<LinearLayout>(R.id.linearbuttons)

            val correctim = findViewById<ImageView>(R.id.correctim)
            val wrongim = findViewById<ImageView>(R.id.wrongim)
            correctim.visibility = View.INVISIBLE
            wrongim.visibility = View.INVISIBLE

            buttonschoicecontainer.visibility = View.INVISIBLE;
            linearbuttons.visibility = View.VISIBLE;
            val listofnotes = listOf("c1", "d1", "e1", "f1", "g1", "a1", "b1", "c2")

            for (note in listofnotes) {
                val id: Int = resources.getIdentifier("choose" + note, "id", this.packageName)
                val btn_by_id = findViewById<Button>(id);
                btn_by_id.visibility = View.INVISIBLE;}

            val notesPlaceHolder = findViewById<ImageView>(R.id.notesplaceholder)

            val notesList = listOf(
                R.drawable.c1,
                R.drawable.d1,
                R.drawable.e1,
                R.drawable.f1,
                R.drawable.g1,
                R.drawable.a1,
                R.drawable.b1,
                R.drawable.c2)
            val notesNamesList = listOf("דו", "רה", "מי", "פה", "סול", "לה", "סי", "דו גבוה")

//            notesnameslist.shuffled()

            var numCorrect = 0
            var curIndex = 0
            var numIncorrect = 0

            fun runNextNote(ind: Int) {
                correctim.visibility = View.INVISIBLE
                wrongim.visibility = View.INVISIBLE

                val curCorrectNote = notesList[ind]
                val correctNoteName = notesNamesList[ind]
                notesPlaceHolder.setImageResource(curCorrectNote);
                buttonschoicecontainer.visibility = View.VISIBLE;
                linearbuttons.visibility = View.VISIBLE;

                for (note in listofnotes) {
                    val id: Int = resources.getIdentifier("choose" + note, "id", this.packageName)
                    val btn_by_id = findViewById<Button>(id);
                    btn_by_id.visibility = View.VISIBLE;
                    btn_by_id.setOnClickListener({
                        if (btn_by_id.text.toString() == correctNoteName) {
                            correctim.visibility = View.VISIBLE; // THIS IS NOT SHOWING
                            Thread.sleep(1_000)
                            curIndex = curIndex + 1
                            numCorrect = numCorrect + 1
                        }
                        else {
                            wrongim.visibility = View.VISIBLE; // THIS IS NOT SHOWING
                            Thread.sleep(1_000)
                            numIncorrect = numIncorrect + 1;
                        }
//                        Thread.sleep(2_000)
                        if (ind < notesNamesList.size - 1) {runNextNote(curIndex)}
                        else {
                            correctim.visibility = View.INVISIBLE
                            wrongim.visibility = View.INVISIBLE
                            val textViewResults = findViewById<TextView>(R.id.textViewResults);
                            textViewResults.visibility = View.VISIBLE;
                            notesPlaceHolder.setImageResource(R.drawable.gcleftreb)
                            textViewResults.text = "Correct: $numCorrect, Wrong: $numIncorrect";
                        }

            }
                );}



            }

            runNextNote(0)

            insets
        }
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật