I have defined a GridView in Android Studio, and I want to get the row and column value of each cell. Basically I am building a Tic Tac Toe kind of app and I want the value of each cell so that I can find if that cell is empty or filled.
int chance = 0;
int[] pos = new int[9];
public void click(View view) {
ImageView counter = (ImageView) view;
if(chance == 0) {
counter.setImageResource(R.drawable.yellow);
chance = 1;
// Here I want to update the that this particular cell is
clicked and it should be updated in pos array.
}
else {
counter.setImageResource(R.drawable.red);
chance = 0;
// Similarly here also it should be updated.
}
}
You can use tags for each component in grid layout in the layout xml file, like :
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:onClick="dropIn"
android:tag="0" />
Then use .getTag()
function to get the component
counter.getTag().toString()