Reputation: 5121
I am making a gridlayout which contains multiple textviews. The xml code for the same is:
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alignmentMode="alignMargins"
android:background="@android:color/white"
android:columnCount="1"
android:columnOrderPreserved="false"
android:rowCount="1">
<LinearLayout
android:id="@+id/row1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="@android:color/white"
android:weightSum="5">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="@android:color/black"
android:textSize="32sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="@android:color/black"
android:textSize="32sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="@android:color/black"
android:textSize="32sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="@android:color/black"
android:textSize="32sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_black"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:textColor="@android:color/black"
android:textSize="32sp" />
</LinearLayout>
</GridLayout>
As you can see in this, for each of the textviews I have defined the onClick method. The click is handled like:
public void onClick(View v) {
if (v instanceof TextView && isInitiatePhase) {
((TextView) v).setText(String.valueOf(++count));
if (count == 5) {
isInitiatePhase = false;
}
} else if (v instanceof TextView) {
// now we have to start cutting/setting the color to red
((TextView) v).setTextColor(Color.RED);
}
}
The initiation is handled fine, now after the initiation phase when I am clicking the TextViews
I want to know which one is clicked. Is there a way to find something like an index of the TextView in the onClick()
method?
Does LinearLayout
provide that functionality, if the TextView
does not?
Also, is my approach correct here? Or is there another Layout that I should use for the same?
Upvotes: 0
Views: 107
Reputation: 970
Instead on adding onClick
, we can add onClickListener
in each child view inside linearlayout of gridlayout. Something like this,
//Add gridlayout id in xml
GridLayout girdLayout = (GridLayout) findViewById(R.id.gridlayout_id);
final LinearLayout ll = gridLayout.findViewById(R.id.row1);
int childCount = ll.getChildCount();
for (final int i= 0; i < childCount; i++){
TextView textView = (TextView) ll.getChildAt(i);
textView.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
// Do your job, i current index of child
}
});
}
Upvotes: 1