I'm creating a tic-tac-toe like game which contains some circular balls that are dropped when user taps on the rooms where he wants to deploy. I have added those images inside a grid layout but I'm not able to run a dedicated method when a user taps on them. I have assigned the method to onclick property of imageview but the java code isn't being executed.

EDIT: I tried to log something when the grid layout was click and it was logged. So the conclusion is that the grid layout is blocking the click to be registered to the imageview.

Here's my Java code:

public void dropIn(View view) {
    ImageView counter = (ImageView) view;
    counter.setTranslationY(1000f);
    counter.setImageResource(R.drawable.yellow);
    counter.animate().translationYBy(1000f).setDuration(300);
}

This is the XML code for the image view:

<ImageView
        android:id="@+id/imageView5"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_margin="10dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:onClick="dropIn"
        app:layout_column="1"
        app:layout_row="1" />

Here is the XML code for the grid:

<android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="360dp"
        android:layout_marginStart="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="185dp"
        android:layout_marginEnd="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginBottom="186dp"
        android:background="@drawable/board"
        app:columnCount="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:rowCount="3">

1 Answers

0
Acemond On Best Solutions

You can try to do it programmatically instead of assigning the method via XML view.

In first place you need to create a new ImageView and then get the view rendered with findViewById() method. Then, assign the onClickListener with the "dropIn" logic:

ImageView counter = findViewById(R.id.imageView5);
counter.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        counter.setTranslationY(1000f);
        counter.setImageResource(R.drawable.yellow);
        counter.animate().translationYBy(1000f).setDuration(300);
    }
 });