Thursday, September 4, 2014

OnGlobalLayoutListener called repeatly, and remove it

In last post, we re-layout the views when the global layout state within the view tree changes, with OnGlobalLayoutListener. But once re-layout the views, it trigger another layout state change, and repeat and repeat again.

To prevent it, we can call removeGlobalOnLayoutListener() (deprecated in API level 16) or removeOnGlobalLayoutListener() (for API Level >= 16) after layout.
package com.example.androidtouchview;

import com.example.androidtouchview.MyView.OnToggledListener;

import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.GridLayout;
import android.widget.Toast;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity 
 implements OnToggledListener{
 int numberOfGlobalLayoutCalled = 0;
 MyView[] myViews;
 GridLayout myGridLayout;

 protected void onCreate(Bundle savedInstanceState) {

  myGridLayout = (GridLayout)findViewById(;
  int numOfCol = myGridLayout.getColumnCount();
  int numOfRow = myGridLayout.getRowCount();
  myViews = new MyView[numOfCol*numOfRow];
  for(int yPos=0; yPos<numOfRow; yPos++){
   for(int xPos=0; xPos<numOfCol; xPos++){
    MyView tView = new MyView(this, xPos, yPos);
    myViews[yPos*numOfCol + xPos] = tView;
   new OnGlobalLayoutListener(){

   public void onGlobalLayout() {
    final int MARGIN = 5;
    int pWidth = myGridLayout.getWidth();
    int pHeight = myGridLayout.getHeight();
    int numOfCol = myGridLayout.getColumnCount();
    int numOfRow = myGridLayout.getRowCount();
    int w = pWidth/numOfCol;
    int h = pHeight/numOfRow;
    for(int yPos=0; yPos<numOfRow; yPos++){
     for(int xPos=0; xPos<numOfCol; xPos++){
      GridLayout.LayoutParams params = 
       (GridLayout.LayoutParams)myViews[yPos*numOfCol + xPos].getLayoutParams();
      params.width = w - 2*MARGIN;
      params.height = h - 2*MARGIN;
      params.setMargins(MARGIN, MARGIN, MARGIN, MARGIN);
      myViews[yPos*numOfCol + xPos].setLayoutParams(params);
     "numberOfGlobalLayoutCalled = " + numberOfGlobalLayoutCalled, 
    //deprecated in API level 16
    //for API Level >= 16

 public void OnToggled(MyView v, boolean touchOn) {

  //get the id string
  String idString = v.getIdX() + ":" + v.getIdY();

   "Toogled:\n" +
   idString + "\n" +



Andrea Petreti said...

Hi, can you show the MYview class?

Andr.oid Eric said...

Hello Andrea Petreti, in last post, Insert view to GridLayout dynamically, with even width and height.

275eaniversarisantana said...

Hello thanks for your help with that code, I'm having some troubles... I implemented it and it works perfect, the thing that I don't understand....
I'm wondering if I can avoid this getViewTreeObserver().addOnGlobalLayoutListener or it must be there on MainActivity to create the GridLayout? the problem is that I don't want t fill of my screen so if I put this android:layout_height="400dp" and I put GridLayout 7x7 it says :vertical constraints: y3-y0>=1749, y3-y2<=400, y2-y1<=400, y1-y0<=400 are inconsistent; permanently removing: y3-y2<=400. Also I'm having problems to get the row column I posted a question on StackOverflow, please help me...


cheesus said...

the solution presented her (removing the listener at the end of onGlobalLayout() did not work for us in cases where the soft keyboard was retracting just when we were layouting, resulting in the layout not growing for the newly added space.
so the solution for us is to execute the layout adjustment when they need to, i.e. when width and/or height change; so on the second call it will exit without triggering layout changes, thus not producing an endless loop.
and if now keyboard hides, call 3 will relayout and call 4 will again just quit.

Marc said...

Thanks for the code!!!

Is there a possibility to toggle all grid elements with an extra Button?