user1506104
user1506104

Reputation: 7086

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?

Upvotes: 3

Views: 1420

Answers (2)

user1506104
user1506104

Reputation: 7086

I don't know but adding android:focusableInTouchMode="true" helped. I added this for my layouts which has editable fields in them such as

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:focusableInTouchMode="true">
    ...
    <EditText
        android:id="@+id/editText"
        android:layout_width="100dp"
        android:layout_height="wrap_content" />
    ...
 </LinearLayout>

Upvotes: 0

Vladimir Jovanović
Vladimir Jovanović

Reputation: 2267

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.

Upvotes: 1

Related Questions