Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinearLayout Flickers Randomly

As the title suggests, a LinearLayout in my app flickers randomly. It is hard to replicate and hard to get a screenshot at the same time. So please bear with me. I could only capture it by taking a picture. Here is the expected behavior of the LinearLayout (Notice the white box below the red header):

enter image description here

Now here is the same page with LinearLayout where the white background disappears:

enter image description here

There are two cases when this happens. First, after the page loads completely. Second, when doing drag events on the smiley faces.

To fix it, I tried to use the Android Studio's code analysis feature to get suggestions on how to further optimize this page's xml layout. The suggestions it gave were: Missing contentDescription attribute on image, alignment options to support right-to-left layouts, set baselineAligned="false" on LinearLayout for better performance.

I added the baselineAligned="false" option for all my LinearLayouts but nothing happened.

In previous posts here in SO, this is the closest I could get. However those cache options for LinearLayout doesn't seem to help. Do you think LinearLayout has optimizations too like what ListView have as described here? Most importantly, how can I solve the issue?

EDIT:

root.getViewTreeObserver().addOnGlobalLayoutListener(
     new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {    
            mWindowManager.updateViewLayout(mRootView,
                                mLayoutParams);

        }
});

To give more context, this app was implemented with services that has UI. The page in the screenshot is a service. (Please don't ask my why. I just joined the team and the original programmer already left.)

UPDATE: I found the issue! Hooray! So when the page loads, the first textbox automatically gets the focus. When I disabled the autofocus on my text field, the issue no longer happens. Can someone please tell me why?

like image 436
user1506104 Avatar asked May 11 '26 03:05

user1506104


1 Answers

I am pretty sure that mWindowManager.updateViewLayout(mRootView, mLayoutParams); is firing onGlobalLayout() method again, because updateViewLayout changes layout, and in documentation for onGlobalLayout() it says:

Callback method to be invoked when the global layout state or the visibility of views within the view tree changes.

So try adding root.getViewTreeObserver().removeGlobalOnLayoutListener(this); before mWindowManager.updateViewLayout(mRootView, mLayoutParams); to avoid recurrent calls of the same method.

UPDATE: Reason why it was happening on focus is because when TextView gets focus it changes background, and sometimes also size, so onGlobalLayout is called. If you remove listener everything should be fine.

like image 66
Vladimir Jovanović Avatar answered May 12 '26 18:05

Vladimir Jovanović



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!