Home > other >  New to coding - Android Dice Roll App with 2 Results
New to coding - Android Dice Roll App with 2 Results

Time:06-15

This is my first post and I am brand new to coding so please let me know if I've missed anything for getting some help.

I'm taking the Google Android Dev tutorials. The tutorial is walking me through creating a dice roll app. I completed that and for an extra challenge practice at the end it recommends getting two results from one button click.

I tried doing that in this code:

package com.example.diceroller

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView

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

        val rollButton: Button = findViewById(R.id.button)
        rollButton.setOnClickListener { rollDice() }
        rollButton.setOnClickListener { rollDice2() }
    }

    private fun rollDice() {
        val dice = Dice(6)
        val diceRoll = dice.roll()
        val resultTextView: TextView = findViewById(R.id.textView)
        resultTextView.text = diceRoll.toString()
    }

    private fun rollDice2() {
        val dice2 = Dice2(6)
        val diceRoll2 = dice2.roll2()
        val resultTextView: TextView = findViewById(R.id.textView2)
        resultTextView.text = diceRoll2.toString()
    }
}

class Dice(private val numSides: Int) {

    fun roll(): Int {
        return (1..numSides).random()
    }
}

class Dice2(private val numSides: Int) {

    fun roll2(): Int {
        return (1..numSides).random()
    }
}

I don't get any errors, but when I run the app it only shows one result (the second result). Again, I'm new to all this and maybe I'll learn it later, but looking for some help on why it only spits out one result. Any help is greatly appreciated and thank you in advance.

CodePudding user response:

You can also achieve the same result by rolling the same dice twice

package com.example.diceroller

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView

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

        val rollButton: Button = findViewById(R.id.button)
        // setOnClickListner -> defines what to execute on button click
        rollButton.setOnClickListener { rollDice() }
    }

    private fun rollDice() {
        // create two dice's each with '6' sides
        var dice_1 = Dice(6)
        var dice_2 = Dice(6)
        
        // roll the two dice's
        val dice_1_roll = dice_1.roll()
        val dice_2_roll = dice_2.roll()
        
        // bind the obtained result to the corresponding 'textView'
        val resultTextView_1: TextView = findViewById(R.id.textView)
        val resultTextView_2: TextView = findViewById(R.id.textView)
        
        // fun roll() in Dice: Class return 'Int' so convert into 'String'
        resultTextView_1.text = dice_1_roll.toString()
        resultTextView_2.text = dice_2_roll.toString()
    }
}

class Dice(private val numSides: Int) {

    fun roll(): Int {
        return (1..numSides).random()
    }
}

CodePudding user response:

It's not made clear by the documentation for setOnClickListener, but a View (which a Button is a type of) can only have one click listener. So when you do this:

rollButton.setOnClickListener { rollDice() }
rollButton.setOnClickListener { rollDice2() }

you're setting a listener that calls rollDice(), and then replacing it with another one that calls rollDice2(). You need to do everything in a single listener!

rollButton.setOnClickListener { 
    rollDice()
    rollDice2()
}

so when you click the button, it'll run the code in that lambda function you're passing in as your listener, so it'll call rollDice() and then rollDice2().


As a general rule, if a function is named like setListener, with a set, then it's usually a single listener that you can set (or unset, usually with null). If it's named something like addListener, with an add, that implies you can add more to what's already there, i.e. you can have multiple listeners or whatever.

I'm not saying this will always be true (always check the documentation, or the source code if you can! See what it's actually doing) but it's a good rule of thumb in my experience - but you should always check if you're unsure!

  • Related