onGlobalLayout() called without apparent layout change

812 Views Asked by At

I have a soft keyboard listener that works well (except for one activity). Here it is.

public static void setKeyboardListener(final Activity activity, final ImageView iv_header){
    final View view =activity.findViewById(android.R.id.content);
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout(){

            //get cached screen height
            String s_heightRoot =StringCacheFactory.stringCache.get("screen_height");
            int heightRoot = (s_heightRoot ==null) ? 0 : FormatFactory.tryParseStringToInt(s_heightRoot);

            //get height when onGlobalLayout is called
            Rect measureNewRect = new Rect();
            view.getWindowVisibleDisplayFrame(measureNewRect);
            int heightNew =measureNewRect.bottom;

            if ((heightNew < heightRoot) || (heightRoot ==0)){
                //screen has shrunk, keyboard open, perform awesome code
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }else{
                //full screen, do nothing
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        }
    });
}

For all activities, the listener waits for a screen change then calls onGlobalLayout(). But one activity, onGlobalLayout() gets called immediately (without a screen change). This activity is fairly unique in that it impliments the following

public class PlacesActivity extends AppCompatActivity implements
    GoogleApiClient.OnConnectionFailedListener,
    GoogleApiClient.ConnectionCallbacks {
    //lotsa code
}

PlacesActivity has an AutoCompleteTextView where the user selects a worldwide city with help from Google Places API. Do these callbacks mess with my onGlobalLayoutListener?

Any other situations where onGLobalLayout() can be called without an apparent layout change?

Does the AutoCompleteTextView trigger the onGlobalLayout()?

0

There are 0 best solutions below