Reputation: 4559
I have created an array of button now on click of each buttons I call onClick method...on that method is it possible to the position of button ie row and column position.. The code I have written is:
Code for crating Array of buttons..
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout=null;
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);
//Create Button
for (i = 0; i<6; i++)
{
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout,param);
for(j=0;j<7;j++)
{
pBtnDay[i][j]=new Button(this);
rowLayout.addView(pBtnDay[i][j],param);
pBtnDay[i][j].setOnClickListener(this);
}
}
return true;
code for onClick Method
Button b = (Button)view;
txtDate.setText(b.getText());
boolean bStartDate=false;
now in onclick method i can getText of each button by using Button b=(Button)view
..so is it possible to get the row and column positions..of that particular button..
Upvotes: 2
Views: 327
Reputation: 23552
As I can understand row and column is just a your concept since LinearLayout does not provide a Table like objects disposition or a row/column lookup method.
You can just use Button.setTag()
and save your j
and x
values:
class ButtonPosition {
int x,y;
public ButtonPosition(int x,int y){
this.x = x;
this.y = y;
}
}
......
for(j=0;j<7;j++){
pBtnDay[i][j]=new Button(this);
rowLayout.addView(pBtnDay[i][j],param);
pBtnDay[i][j].setOnClickListener(this);
//Save the position
pBtnDay[i][j].setTag(new ButtonPosition(i,j));
}
Position lookup into the onClick
method
.....
Button b = (Button)view;
ButtonPosition p = (ButtonPosition)b.getTag();
.....
Upvotes: 2