I have 6 image buttons with drawables, I'm trying to compare them using getDrawable(). Here's my xml code
'''
< ?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TicTacToe">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="25dp"
android:layout_marginTop="147dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="14dp"
android:onClick="change"
app:layout_constraintBottom_toTopOf="@+id/imageButton2"
app:layout_constraintEnd_toStartOf="@+id/imageButton01"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/blank" />
<ImageButton
android:id="@+id/imageButton01"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="147dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="14dp"
android:onClick="change"
app:layout_constraintBottom_toTopOf="@+id/imageButton02"
app:layout_constraintEnd_toStartOf="@+id/imageButton001"
app:layout_constraintStart_toEndOf="@+id/imageButton1"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/blank" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="6dp"
android:layout_marginBottom="10dp"
android:onClick="change"
app:layout_constraintBottom_toTopOf="@+id/imageButton3"
app:layout_constraintEnd_toStartOf="@+id/imageButton02"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageButton1"
app:srcCompat="@drawable/blank" />
<ImageButton
android:id="@+id/imageButton001"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="148dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="13dp"
android:onClick="change"
app:layout_constraintBottom_toTopOf="@+id/imageButton002"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageButton01"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/blank" />
<ImageButton
android:id="@+id/imageButton02"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="7dp"
android:layout_marginBottom="10dp"
android:onClick="change"
app:layout_constraintBottom_toTopOf="@+id/imageButton03"
app:layout_constraintEnd_toStartOf="@+id/imageButton002"
app:layout_constraintStart_toEndOf="@+id/imageButton2"
app:layout_constraintTop_toBottomOf="@+id/imageButton01"
app:srcCompat="@drawable/blank" />
<ImageButton
android:id="@+id/imageButton002"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="14dp"
android:layout_marginBottom="10dp"
android:onClick="change"
app:layout_constraintBottom_toTopOf="@+id/imageButton003"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageButton02"
app:layout_constraintTop_toBottomOf="@+id/imageButton001"
app:srcCompat="@drawable/blank" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="6dp"
android:layout_marginBottom="200dp"
android:onClick="change"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageButton03"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageButton2"
app:srcCompat="@drawable/blank" />
<ImageButton
android:id="@+id/imageButton03"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="7dp"
android:layout_marginBottom="200dp"
android:onClick="change"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageButton003"
app:layout_constraintStart_toEndOf="@+id/imageButton3"
app:layout_constraintTop_toBottomOf="@+id/imageButton02"
app:srcCompat="@drawable/blank" />
<ImageButton
android:id="@+id/imageButton003"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="14dp"
android:layout_marginBottom="200dp"
android:onClick="change"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageButton03"
app:layout_constraintTop_toBottomOf="@+id/imageButton002"
app:srcCompat="@drawable/blank" />
</androidx.constraintlayout.widget.ConstraintLayout
'''
kotlin code
'''
class TicTacToe : AppCompatActivity() {
var spot1: ImageButton? =null
var spot01: ImageButton? =null
var spot001: ImageButton? =null
var spot2: ImageButton? =null
var spot02: ImageButton? =null
var spot002: ImageButton? =null
var spot3: ImageButton? =null
var spot03: ImageButton? =null
var spot003: ImageButton? =null
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tic_tac_toe)
spot1 = findViewById<ImageButton>(R.id.imageButton1)
spot01 = findViewById<ImageButton>(R.id.imageButton01)
spot001 = findViewById<ImageButton>(R.id.imageButton001)
spot2 = findViewById<ImageButton>(R.id.imageButton2)
spot02 = findViewById<ImageButton>(R.id.imageButton02)
spot002 = findViewById<ImageButton>(R.id.imageButton002)
spot3 = findViewById<ImageButton>(R.id.imageButton3)
spot03 = findViewById<ImageButton>(R.id.imageButton03)
spot003 = findViewById<ImageButton>(R.id.imageButton003)
}
val photos: IntArray= intArrayOf(R.drawable.x, R.drawable.o, R.drawable.blank)
var turn = 1
var winState = false
fun change(view: View){
var spot = findViewById<ImageButton>(view.getId()) //as Button
if(turn == 1) {
spot.setImageResource(photos[0])
turn = turn * -1
}
else{
spot.setImageResource(photos[1])
turn = turn * -1
}
if(spot1?.getDrawable() == spot01?.getDrawable() &&
spot1?.getDrawable() == spot001?.getDrawable() &&
spot1?.getDrawable() != photos[2].toDrawable())
{
winState = true;
Toast.makeText(getApplicationContext(),"Winner",Toast.LENGTH_SHORT).show();
}
}
}
'''
Drawable
s like this, they will return differentDrawable
objects and thus the comparison will always be false. What I would do is make a classSpot
withImageButton
andDrawable
fields and use the sameDrawable
object to initializeSpot
s whoseDrawable
is the same. Then compare these fields; as they were initialized with same object the comparison should return true. – Cole Tustin