VoidCatz
VoidCatz

Reputation: 365

Android GridLayout get row/column

I have a 3x3 GridLayout with buttons. These buttons have a onTileClicked() listener. Now I want to get the position of the clicked button in the grid. Here is my code:

public void onTileClicked(View view) {
    Button button = (Button) view;
    GridLayout.LayoutParams params = (LayoutParams) button.getLayoutParams();
}

But how can i get the row and column of the clicked button? There is params.columnSpec but I don't know what to do with this...

Upvotes: 7

Views: 13824

Answers (4)

Xavier Xing
Xavier Xing

Reputation: 31

I am facing the same problem and currently the only solution that makes me comfortable is to modify the class and add whatever information you need. My example is

class ImageView extends android.widget.ImageView {
    public ImageView(Context context) {
        super(context);
    }

    //the position of the plane head.
    int xPos,yPos;

    public void setPosInGrid(int x,int y) {
        xPos=x;yPos=y;
    }

    public int[] getPosInGrid() {
        int[] pos=new int[2];
        pos[0]=xPos;
        pos[1]=yPos;
        return pos;
    }
}

And now we can use this additional functionality. We can also rewrite the setLayoutParams and integrate the setPosInGrid() into it. No modification of other codes is required except when you want an ordinary ImageView just use the full identifier "androi.widget.ImageView".

Upvotes: 0

LSV
LSV

Reputation: 11

This may be old, but i also looked for a solution and still did not find it. I needed to make multiple lines of button depending on users choice for a game. Using ID's was not possible because the number of buttons could vary from 2x2 to 9x9.

My button did not need to have a caption, so here is what i did:

layout = (GridLayout) findViewById(R.id.layoutPrincipal);
    context = this;        
    for(int i=0;i<rowCount;i++){
        for(int j=0;j<columnCount;j++){
            Button mButon= new Button(this);
            mButon.setText(""+i+""+j);
            mButon.setTextColor(Color.TRANSPARENT);

            mButon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Button button = (Button) v;


                    //button.getText() allows me to retrieve the coordinates.

                    button.getText()

                 }
            });     
            layout.addView(mButon);

        }
    }

Of course this won't work if you need a text on your button. Maybe you could use a tag?

Upvotes: 1

VoidCatz
VoidCatz

Reputation: 365

Thanks for your answer AmmarCSE, but I solved the broblem by just doing something like this:

public void onTileClicked(View view) {
    Button button = (Button) view;
    if(button.getId()==R.id.btn00)onTileClicked(0, 0, button);
    else if(button.getId()==R.id.btn01)onTileClicked(0, 1, button);
    else if(button.getId()==R.id.btn02)onTileClicked(0, 2, button);
    else if(button.getId()==R.id.btn10)onTileClicked(1, 0, button);
    else if(button.getId()==R.id.btn11)onTileClicked(1, 1, button);
    else if(button.getId()==R.id.btn12)onTileClicked(1, 2, button);
    else if(button.getId()==R.id.btn20)onTileClicked(2, 0, button);
    else if(button.getId()==R.id.btn21)onTileClicked(2, 1, button);
    else if(button.getId()==R.id.btn22)onTileClicked(2, 2, button);
}

Upvotes: 2

AmmarCSE
AmmarCSE

Reputation: 30607

There is no direct way to get the position of the cell. However, you can calculate the position of the clicked button assuming you know the number of columns and rows in the onItemClick listener:

    public void onItemClick(AdapterView parent, 
         View v, int position, long id) 
    {   
        int row_no=position/3;
        int col_no=position%3;
        .
        .
    }

Upvotes: 5

Related Questions