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
}
}
}