Set onClickListener for custom view
<p>I have created a custom view called Cell that draws a square. In my activity I create a 2d array of cells, hence giving a grid structure. Now the reason for this is, I want individual cells to respond to my clicks based on some value that they have. Say for eg, each cell has a boolean, and based on true of false I will color the cell.I tried doing this with one cell first. But the strange part is, the click event is triggered even when I click outside the cell.
More Information : I  am creating all view in the onCreate method of the activity. 
Let me know if you need more information.</p>

<p>Thanks in advance for your help !
Thought I will edit my original question for other references :) .</p>

<pre><code>Activity  

onCreate() {
super.onCreate(savedInstanceState);
LinearLayout masterLayout = new LinearLayout(this);
LinearLayout.LayoutParams params;
params =  new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,     
LinearLayout.LayoutParams.MATCH_PARENT);
masterLayout.setLayoutParams(params);
addCells();
setContentView(masterLayout);
}

function addCells() {
for(int i =0; i&lt;2;i++) {
Cell cell = new Cell(this,i);
LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
cell.setLayoutParams(viewParams);
masterLayout.addView(cell);

cell.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//Do Something here

}
});}}

Custom View :

Cell extends View {
boolean flag=false;
int cellNumber;
float xCoordinate=50;
float yCoordinate=50;

Cell(Context context, AttributeSet attrs) {
super(context, attrs);
}
Cell (Context context, int i) {
super(context);
cellNumber = i;
}

onDraw(Canvas c) {
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.STROKE);
xCoordinate = xCoordinate + 40*cellNumber;
c.drawRect(xCoordinate,yCoordinate,xCoordinate+40,yCoordinate+40, paint);

}}
</code></pre>

<p>This is a much simpler version that I was trying out. Now the strange part is, though I am adding two different instances of my custom view, the onDraw() is called just once (on exiting the onCreate() of the activity). From what I read, the onDraw() is called for every new view render. Please enlighten me on that front !</p>

<p>Thanks a lot!</p>
