I have created a custom ViewGroup in which I create a couple of custom child Views (in the java code, not xml).
Each of these childs needs to have the same onClick event.
The method to handle these click events resides in the activity class that uses the layout. How do I set this method as the onClick handler for all child views?
Here is my code simplified.
The custom ViewGroup:
public class CellContainerGroup extends ViewGroup
{
CellView[][] CellGrid = new CellView[9][9];
public CellContainerGroup(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(R.styleable.CellContainerGroup);
try {
//initialize attributes here.
} finally {
a.recycle();
}
for(int i = 0; i < 9 ; i++)
for(int j = 0 ; j < 9 ; j++)
{
//Add CellViews to CellGrid.
CellGrid[i][j] = new CellView(context, null); //Attributeset passed as null
//Manually set attributes, height, width etc.
//...
//Add view to group
this.addView(CellGrid[i][j], i*9 + j);
}
}
}
The activity that contains the method I want to use as the click handler:
public class SudokuGameActivity extends Activity {
//Left out everything else from the activity.
public void cellViewClicked(View view) {
{
//stuff to do here...
}
}
Setup a single
OnClickListener(inCellContainerGroup) which will call that method:where
propagateEventis a method inCellContainerGrouplike this:and where
mContextis aContextreference like this:Don’t forget to set the
mListener: