Display a random layout in Kotlin

I am trying to create an app that display random challenges in different levels of difficulty to build a gamified self-development app. As i am a pretty unexperienced developer(this is my first app that is not part of any course), I didn’t used fragments but i actually created a layout that correspond to every challenge. In my app, i am displaying as MainActivity the different levels of difficulty which are represented by buttons. Each of those level button create an intent to an introductory page to the level which also contains a button at the bottom that should randomly select one of the layouts(categorized as part of this level) and display it to the user. My problem is that i don’t know the code to do this kind of selection.

I tried to do this:

 private val SafeChallenges = listOf(
    DeclutterPhone::class,
    Drink2glasses::class,
    TodoList::class
)

private fun startRandomActivity() {
    startActivity(Intent(this, SafeChallenges.random().java))
 
}   

But it didn’t worked out. I created 3 classes to have each one with a setContentView to a specific layout(challenge).

I also tried this type of loop but my button doesn’t respond to it.

 override fun onClick(view: View){
  Log.d(TAG,"onclick: called")
    category12_challenge_button.setOnClickListener {
        Log.d(TAG,"button clicked")
        val myRandomChallenges = Random.nextInt(1..3)

        when(myRandomChallenges){
            1->startActivity(Intent(this,DeclutterPhone::class.java))
            2->startActivity(Intent(this,Drink2glasses::class.java))
            3->startActivity(Intent(this,TodoList::class.java))
            else -> IllegalArgumentException("unknown layout")
        }
    }

Could you tell me where I missed my point?

Thanks a lot!

This it probably better suited for stackoverflow as a question.

Did you mean 3 activities classes?

I don’t really know what you’re doing here, but this is (I think) an onclick listener which sets the onClick listerner for another button, I have no idea why you’re doing that, but this is most likely not correct, it means you have to click these 2 buttons in a specific order for anything to happen.

Said that, when you say “it didn’t work out” it’s very unclear what you mean.
Did you get a crash? If so, find out what crashed.
Did nothing happen? Check your listeners, find out what code was executed and what wasn’t.

1 Like

By "it didn’t worked out i mean: The button didn’t reacted. I tried stackoverflow but my question was replied incorrectly because i didn’t made my point very well. I corrected my question, tried new things and documented everything. But when i finally aligned my question with “quality standards” urged by moderators, no one was answering my question anymore and i am still stuck.
Do you know how could i achieve this piece of code correctly?

I must say, your question is confusing. So you started talking about picking random activities.
Now it seems the issue is that a button callback doesn’t fire.

I’ve asked you why you’re setting a listener inside an 'onClick` you still haven’t answered to that.

Also you say nothing happened, you do have log statements did those run? What do you have in the logs?

The log statements didn’t appearedon the Logcat like they weren’t called when i ran the app. I tried an OnsetClicklistener because i needed a reference to my button and R.id showed me an error inside OnClikc with my when loop. I am a beginner, i am trying to build a first app but it seems to be very complicated to do that feature.

Without seeing the rest of your code is hard to understand what you’re doing.

As I read this question the issue is “I can’t set an onclick listener for a button”.

In general you set listners in an activity onCreate method, I suggest you move your setOnClickListener there. I don’t know what is this onClick method you’re showing here, what class is it even in?

Alright, thanks you al3c. I am trying to get as much info to then build up my code. Thanks for the reference to the other issue;)