Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 745
VoidCatz Avatar asked Apr 18 '14 12:04

VoidCatz


3 Answers

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;
        .
        .
    }
like image 195
AmmarCSE Avatar answered Nov 15 '22 22:11

AmmarCSE


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);
}
like image 2
VoidCatz Avatar answered Nov 16 '22 00:11

VoidCatz


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?

like image 1
LSV Avatar answered Nov 15 '22 22:11

LSV