Sanjeev Jha
Sanjeev Jha

Reputation: 81

Get ImageView child from GridLayout and clear them?

I am trying to build Tic Tac Toe app using a tutorial.I am stuck at one part.I have a GridLayout having 9 ImageView in 3 rows and 3 column.Have a look at my activity_main xml file

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MainActivity">

<android.support.v7.widget.GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="315dp"
    android:layout_height="333dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:background="@drawable/matrix"
    app:columnCount="3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.454"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.562"
    app:rowCount="3">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="82dp"
        android:layout_height="70dp"
        android:layout_marginTop="18dp"
        android:layout_marginBottom="10dp"
        android:onClick="onclick"
        android:tag="0"
        app:layout_column="0"
        app:layout_row="0" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="130dp"
        android:layout_height="73dp"
        android:layout_marginTop="15dp"
        android:onClick="onclick"
        android:tag="1"
        app:layout_column="1"
        app:layout_row="0" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="87dp"
        android:layout_height="74dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:onClick="onclick"
        android:tag="2"
        app:layout_column="2"
        app:layout_row="0" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="82dp"
        android:layout_height="70dp"
        android:layout_marginTop="18dp"
        android:layout_marginBottom="10dp"
        android:onClick="onclick"
        android:tag="3"
        app:layout_column="0"
        app:layout_row="1" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="130dp"
        android:layout_height="73dp"
        android:layout_marginTop="15dp"
        android:onClick="onclick"
        android:tag="4"
        app:layout_column="1"
        app:layout_row="1" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="87dp"
        android:layout_height="74dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:onClick="onclick"
        android:tag="5"
        app:layout_column="2"
        app:layout_row="1" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="82dp"
        android:layout_height="70dp"
        android:layout_marginTop="22dp"
        android:layout_marginBottom="10dp"
        android:onClick="onclick"
        android:tag="6"
        app:layout_column="0"
        app:layout_row="2" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="130dp"
        android:layout_height="73dp"
        android:layout_marginTop="20dp"
        android:onClick="onclick"
        android:tag="7"
        app:layout_column="1"
        app:layout_row="2" />

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="87dp"
        android:layout_height="74dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="15dp"
        android:onClick="onclick"
        android:tag="8"
        app:layout_column="2"
        app:layout_row="2" />
</android.support.v7.widget.GridLayout>

<LinearLayout
    android:id="@+id/linearlayout"
    android:layout_width="276dp"
    android:layout_height="176dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    android:visibility="invisible"
    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.355">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="93dp"
        android:text="TextView"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:onClick="playagain"
        android:text="@string/button"
        android:textSize="24sp" />
</LinearLayout>

To empty the already drawn image in GridLayout I used this.But the app's getting crashed can anyone tell me if it's correct

enter code here
public void playagain(View view)
 {
   GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);

    for (int i = 0; i< gridLayout.getChildCount(); i++)
    {
        ((ImageView)gridLayout.getChildAt(i)).setImageResource(0);
    }

 }

Here's the image of my logcat

Upvotes: 0

Views: 400

Answers (2)

S41Z
S41Z

Reputation: 1

Sup? You can try to change your code from

GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout); to android.support.v7.widget.GridLayout gridLayout = (android.support.v7.widget.GridLayout)findViewById(R.id.gridLayout); Use

android.support.v7.widget.GridLayout

instead of

GridLayout

Upvotes: 0

Kiran
Kiran

Reputation: 51

You have to import android.support.v7.widget.GridLayout instead of importing android.widget.GridLayout in your class.

Upvotes: 1

Related Questions