Home>
The problem I want to solve

The image I set when I started the emulator is not displayed, so I want to be able to display it.

Challenges facing

I want to be able to display irasutoyahappy.png or irasutoyabad.png when I win three times or lose three times with this program, and I want to display the "Next" button every time the rock-paper-scissors game ends.
If i do not have enough information, please let us know as we will post it.

Code /// mainactivity ///
package com.example.janken
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.preference.PreferenceManager
import androidx.core.content.edit
import kotlinx.android.synthetic.main.activity_main. *
import android.view.View as View1
class MainActivity: AppCompatActivity () {
    class MainActivity: AppCompatActivity () {
        override fun onCreate (savedInstanceState: Bundle?) {
            super.onCreate (savedInstanceState)
// setContentView (R.layout.activity_test3)
            setContentView (R.layout.activity_main)
            gu.setOnClickListener {onJankenButtonTapped (it)}
            choki.setOnClickListener {onJankenButtonTapped (it)}
            pa.setOnClickListener {onJankenButtonTapped (it)}
            val pref = PreferenceManager.getDefaultSharedPreferences (this)
            pref.edit {
                clear ()
            }
        }
        fun onJankenButtonTapped (view: View1?) {
            val streak = this.intent.getIntExtra ("streak", 0)
            val intent = Intent (this, ResultActivity :: class.java)
            intent.putExtra ("MY_HAND", view? .id)
            intent.putExtra ("streak", streak)
            startActivity (intent)
        }
    }
}
Code /// resultactivity ///
package com.example.janken
import android.content.Intent
import android.os.Bundle
import android.preference.PreferenceManager
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit
import kotlinx.android.synthetic.main.activity_result. *
class ResultActivity: AppCompatActivity () {
    val gu = 0
    val choki = 1
    val pa = 2
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_result)
        val id = intent.getIntExtra ("MY_HAND", 0)
        val myHand: Int
        myHand = when (id) {
            R.id.gu->{myHandImage.setImageResource (R.drawable.gu)
                gu
            }
            R.id.choki->{
                myHandImage.setImageResource (R.drawable.choki)
                choki
            }
            R.id.pa->{
                myHandImage.setImageResource (R.drawable.pa)
                pa
            }
            else->gu
        }
        // Decide on a computer hand
        val comHand = getHand ()
        when (comHand) {
            gu->comHandImage.setImageResource (R.drawable.com_gu)
            choki->comHandImage.setImageResource (R.drawable.com_choki)
            pa->comHandImage.setImageResource (R.drawable.com_pa)
        }
        // Judge victory or defeat
        val gameResult = (comHand --myHand + 3)% 3
        when (gameResult) {
            0->resultLabel.setText (R.string.result_draw) // Draw
            1->resultLabel.setText (R.string.result_win) // If i win
            2->resultLabel.setText (R.string.result_lose) // If i lose
        }
        backButton.setOnClickListener {finish ()}
        var streak = intent.getIntExtra ("streak", 0)
        when (gameResult) {
            0->{
                resultLabel.setText (R.string.result_draw) // Draw
                // Return the count to 0 in case of a draw
                streak = 0
            }
            1->{
                resultLabel.setText (R.string.result_win) // If i win
                // Return the value to 0 if i am losing in a row
                if (streak<0) {
                    streak = 0
                }
                streak + = 1
            }
            2->{
                // Return the value to 0 if i am winning in a row
                if (streak>0) {
                    streak = 0
                }
                streak-= 1
                resultLabel.setText (R.string.result_lose) // If i lose
            }
        }
        // TODO: The program ends. What do I want to do, do I want to change the transition destination of NEXT? Or do I want to change the display?
        if (streak>= 3 || streak<= -3) {
            Log.d ("TEST", "End of game")
        }
        nextButton.setOnClickListener {
            val intent = Intent (this, MainActivity :: class.java)intent.putExtra ("streak", streak)
            // TODO: Avoiding continuous activity stacking. Correct if you need to think about the screen configuration
            intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP)
            startActivity (intent)
        }
        saveData (myHand, comHand, gameResult)
    }
    private fun saveData (myHand: Int, comHand: Int, gameResult: Int) {
        val pref = PreferenceManager.getDefaultSharedPreferences (this)
        val gameCount = pref.getInt ("GAME_COUNT", 0)
        val winningStreakCount = pref.getInt ("WINNING_STREAK_COUNT", 0)
        val lastComHand = pref.getInt ("LAST_COM_HAND", 0)
        val lastGameResult = pref.getInt ("GAME_RESULT", -1)
        val edtWinningStreakCount: Int =
            when {
                lastGameResult == 2&&gameResult == 2->                  winningStreakCount + 1
                else->                  0
            }
        val editor = pref.edit ()
        pref.edit {
            putInt ("GAME_COUNT", gameCount + 1)
            putInt ("WINNING_STREAK_COUNT", edtWinningStreakCount)
            putInt ("LAST_MY_HAND", myHand)
            putInt ("LAST_COM_HAND", comHand)
            putInt ("BEFORE_LAST_COM_HAND", lastComHand)
            putInt ("GAME_RESULT", gameResult)
        }
    }
    private fun getHand (): Int {
        var hand = (Math.random () * 3) .toInt ()
        val pref = PreferenceManager.getDefaultSharedPreferences (this)
        val gameCount = pref.getInt ("GAME_COUNT", 0)
        val winningStreakCount = pref.getInt ("WINNING_STREAK_COUNT", 0)
        val lastMyHand = pref.getInt ("LAST_MY_HAND", 0)
        val lastComHand = pref.getInt ("LAST_COM_HAND", 0)
        val beforeLastComHand = pref.getInt ("BEFORE_LAST_COM_HAND", 0)
        val gameResult = pref.getInt ("GAME_RESULT", -1)
        if (gameCount == 1) {
            if (gameResult == 2) {
                // If the last game was the first and the computer won
                // Computer changes next move
                while (lastComHand == hand) {
                    hand = (Math.random () * 3) .toInt ()
                }
            } else if (gameResult == 1) {
                // If the last game was the first and the computer lost
                // Put out a hand that beats the opponent's hand
                hand = (lastMyHand --1 + 3)% 3
            }
        } else if (winningStreakCount>0) {
            if (beforeLastComHand == lastComHand) {
                // Change hands if you win consecutively with the same hand
                while (lastComHand == hand) {
                    hand = (Math.random () * 3) .toInt ()
                }
            }}
        return hand
    }
}
Code /// activity_main.xml ///<? xml version = "1.0" encoding = "utf-8"?><ImageButton
        android: id = "@ + id/choki"
        android: layout_width = "100dp"
        android: layout_height = "100dp"
        android: layout_marginStart = "8dp"
        android: layout_marginTop = "8dp"
        android: layout_marginEnd = "8dp"
        android: layout_marginBottom = "8dp"
        android: scaleType = "fitCenter"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toTopOf = "parent"
        app: srcCompat = "@ drawable/choki" /><ImageButton
        android: id = "@ + id/pa"
        android: layout_width = "100dp"
        android: layout_height = "100dp"
        android: layout_marginStart = "8dp"
        android: scaleType = "fitCenter"
        app: layout_constraintStart_toEndOf = "@ + id/choki"
        app: layout_constraintTop_toTopOf = "@ + id/choki"
        app: srcCompat = "@ drawable/pa" /><ImageButton
        android: id = "@ + id/gu"
        android: layout_width = "100dp"
        android: layout_height = "100dp"
        android: layout_marginEnd = "8dp"
        android: scaleType = "fitCenter"
        app: layout_constraintEnd_toStartOf = "@ + id/choki"
        app: layout_constraintTop_toTopOf = "@ + id/choki"
        app: srcCompat = "@ drawable/gu" /><TextView
        android: id = "@ + id/textView"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "8dp"
        android: layout_marginTop = "8dp"
        android: layout_marginEnd = "8dp"
        android: layout_marginBottom = "8dp"
        android: text = "@ string/janken_text"
        android: textAppearance = "@ style/TextAppearance.AppCompat.Large"
        app: layout_constraintBottom_toTopOf = "@ + id/choki"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toTopOf = "parent" /></androidx.constraintlayout.widget.ConstraintLayout>
  • Answer # 1

    I can't paste the screenshot, so I'll paste it here.
    If you press Code or Split for the xml file, the code will come out, so please press that.
    (The answer will be added separately after checking the status of xml etc.)

    === Addendum
    In activity_result.xmltools: layout_editor_absoluteXOrtools: layout_editor_absoluteYIs used.
    This looks like it's laid out on the tool, but it doesn't make sense when run.
    I tried to rewrite it so that it looks the same as on the display, but can the problem be solved by doing the following?

    <? xml version = "1.0" encoding = "utf-8"?><TextView
            android: id = "@ + id/resultLabel"
            android: layout_width = "wrap_content"
            android: layout_height = "wrap_content"
            android: layout_marginStart = "8dp"
            android: layout_marginTop = "8dp"
            android: layout_marginEnd = "8dp"
            android: layout_marginBottom = "8dp"
            android: text = "TextView"
            android: textAppearance = "@ style/TextAppearance.AppCompat.Large"
            app: layout_constraintBottom_toBottomOf = "parent"
            app: layout_constraintEnd_toEndOf = "parent"
            app: layout_constraintStart_toStartOf = "parent"
            app: layout_constraintTop_toTopOf = "parent" /><ImageView
            android: id = "@ + id/myHandImage"
            android: layout_width = "160dp"
            android: layout_height = "160dp"
            android: layout_marginStart = "8dp"
            android: layout_marginTop = "8dp"
            android: layout_marginEnd = "8dp"
            android: layout_marginBottom = "8dp"
            android: scaleType = "fitCenter"
            app: layout_constraintBottom_toTopOf = "@ + id/resultLabel"
            app: layout_constraintEnd_toEndOf = "parent"
            app: layout_constraintStart_toStartOf = "parent"
            app: layout_constraintTop_toTopOf = "parent"
            app: srcCompat = "@ drawable/gu" /><ImageView
            android: id = "@ + id/comHandImage"
            android: layout_width = "130dp"
            android: layout_height = "200dp"
            android: layout_marginStart = "8dp"
            android: layout_marginTop = "8dp"
            android: layout_marginEnd = "8dp"
            android: layout_marginBottom = "8dp"
            android: scaleType = "fitCenter"
            app: layout_constraintBottom_toBottomOf = "parent"
            app: layout_constraintEnd_toEndOf = "parent"
            app: layout_constraintStart_toStartOf = "parent"
            app: layout_constraintTop_toBottomOf = "@ + id/resultLabel"
            app: srcCompat = "@ drawable/com_gu" /><Button
            android: id = "@ + id/backButton"
            android: layout_width = "wrap_content"android: layout_height = "wrap_content"
            android: layout_marginStart = "16dp"
            android: layout_marginTop = "4dp"
            android: text = "@ string/back_text"
            app: layout_constraintStart_toStartOf = "parent"
            app: layout_constraintTop_toTopOf = "parent" /><ImageButton
            android: id = "@ + id/imageButton3"
            android: layout_width = "100dp"
            android: layout_height = "100dp"
            android: scaleType = "fitCenter"
            app: layout_constraintBottom_toBottomOf = "parent"
            app: layout_constraintEnd_toStartOf = "@ + id/comHandImage"
            app: layout_constraintTop_toBottomOf = "@ + id/resultLabel"
            app: srcCompat = "@ drawable/irasutoyahappy" /><ImageButton
            android: id = "@ + id/imageButton4"
            android: layout_width = "100dp"
            android: layout_height = "100dp"
            android: scaleType = "fitCenter"
            app: layout_constraintBottom_toBottomOf = "parent"
            app: layout_constraintStart_toEndOf = "@ + id/comHandImage"
            app: layout_constraintTop_toBottomOf = "@ + id/resultLabel"
            app: srcCompat = "@ drawable/irasutoyahappy" /><Button
            android: id = "@ + id/nextButton"
            android: layout_width = "wrap_content"
            android: layout_height = "wrap_content"
            android: text = "Next"
            app: layout_constraintTop_toTopOf = "parent"
            app: layout_constraintRight_toRightOf = "parent"
            android: layout_marginEnd = "16dp"
            android: layout_marginTop = "4dp"
             /></androidx.constraintlayout.widget.ConstraintLayout>

    Thank you for your cooperation.

    === Addendum
    It is said that it will be pure white, but since MainActivity is written in duplicate, please delete one

      class MainActivity: AppCompatActivity () {
            override fun onCreate (savedInstanceState: Bundle?) {
                super.onCreate (savedInstanceState)
                setContentView (R.layout.activity_main)
                gu.setOnClickListener {onJankenButtonTapped (it)}
                choki.setOnClickListener {onJankenButtonTapped (it)}
                pa.setOnClickListener {onJankenButtonTapped (it)}
                val pref = PreferenceManager.getDefaultSharedPreferences (this)
                pref.edit {
                    clear ()
                }
            }
            fun onJankenButtonTapped (view: View1?) {
                val streak = this.intent.getIntExtra ("streak", 0)
                val intent = Intent (this, ResultActivity :: class.java)
                intent.putExtra ("MY_HAND", view? .id)
                intent.putExtra ("streak", streak)
                startActivity (intent)
            }
        }

    === Addendum
    activity_main.xmlOf the top layer ofConstraintLayoutAt

    android: layout_width = "match_parent"
    android: layout_height = "match_parent"


    Is missing. I wroteactivity_main.xmlPlease refer to and try to put it in.

    <? xml version = "1.0" encoding = "utf-8"?><Button
            android: id = "@ + id/choki"
            android: layout_width = "100dp"
            android: layout_height = "100dp"
            android: layout_marginStart = "8dp"
            android: layout_marginTop = "8dp"
            android: layout_marginEnd = "8dp"
            android: layout_marginBottom = "8dp"
            android: scaleType = "fitCenter"
            app: layout_constraintBottom_toBottomOf = "parent"
            app: layout_constraintEnd_toEndOf = "parent"
            app: layout_constraintStart_toStartOf = "parent"
            app: layout_constraintTop_toTopOf = "parent"
            app: srcCompat = "@ drawable/choki" /><ImageButton
            android: id = "@ + id/pa"
            android: layout_width = "100dp"
            android: layout_height = "100dp"
            android: layout_marginStart = "8dp"
            android: scaleType = "fitCenter"
            app: layout_constraintStart_toEndOf = "@ + id/choki"
            app: layout_constraintTop_toTopOf = "@ + id/choki"
            app: srcCompat = "@ drawable/pa" /><ImageButton
            android: id = "@ + id/gu"
            android: layout_width = "100dp"
            android: layout_height = "100dp"
            android: layout_marginEnd = "8dp"
            android: scaleType = "fitCenter"
            app: layout_constraintEnd_toStartOf = "@ + id/choki"
            app: layout_constraintTop_toTopOf = "@ + id/choki"
            app: srcCompat = "@ drawable/gu" /><TextView
            android: id = "@ + id/textView"
            android: layout_width = "wrap_content"
            android: layout_height = "wrap_content"
            android: layout_marginStart = "8dp"
            android: layout_marginTop = "8dp"
            android: layout_marginEnd = "8dp"
            android: layout_marginBottom = "8dp"
            android: text = "@ string/janken_text"
            android: textAppearance = "@ style/TextAppearance.AppCompat.Large"
            app: layout_constraintBottom_toTopOf = "@ + id/choki"
            app: layout_constraintEnd_toEndOf = "parent"
            app: layout_constraintStart_toStartOf = "parent"
            app: layout_constraintTop_toTopOf = "parent" /></androidx.constraintlayout.widget.ConstraintLayout>