Search code examples
androidandroid-viewandroid-gridlayout

Android - How to get a view's x and y coordinates into a gridLayout?


the actual wondering is the following : I have a full-screen GridLayout which has half of it visible. To see the second part we have to scroll.

I tried some ways and looked around for answers but none of whichs I found gave me what I need. In my grid there are square cells and each one contains a view, and I need to get the x and the y of these views.

Problem is that I could get the coordinates of the views that were put into the visible cells, the views that are not displayed have a x and a y set to 0... Whereas they were drawn..

Here it is, hope some of you guys could help! :)

private void createGrid(){
    //I call the function several times
    int gridsLength = 19*48*grids;

    for(int i = 0; i < 19; i++) {
        for (int j = 0; j < 48; j++) {
            GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
            layoutParams.width = 40;
            layoutParams.height = 40;
            View view = new View(this);
            view.setLayoutParams(layoutParams);
            Util.setDrawableBackground(view, R.drawable.border_black, this);
            view.setOnTouchListener(newViewListener());
            gridViews.add(view);
            //glMain is my gridLayout that i put into global variable
            glMain.addView(view, i + j + gridsLength);
        }
    }

    grids++;
}

Solution

  • EDIT : I finally found out that I was not always accessing the views AFTER the grid were drawn so I changed that and it's working... Figured out that I was just a fool ^^