0

If I want to show a View with content which takes time to fetch, I'd usually include it in a FrameLayout with a ProgressBar alongside. Show the progressBar while fetching and show the view once the content become available.

Example with a textView.

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"
        android:clickable="true">

        <TextView
            android:id="@+id/addressTv"
            android:layout_width="match_parent"
            android:minLines="4"
            android:maxLines="4"
            android:layout_height="match_parent"/>

        <ProgressBar
            android:id="@+id/addressLoadingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:indeterminate="true" />
</FrameLayout>

In this example only one of 'addressTv' or 'addressLoadingBar' is showing at any given moment, the other one is 'GONE'.

But I can't figure out how to make the FrameLayout have a fixed height. In this case the max of the children's heights. With the above layout, it changes when we switch between the children.

kaiser
  • 1
  • 1
  • 1

2 Answers2

0

I suggest switching to RelativeLayout, with both children being visible when the layout is inflated. Then set the one not needed to invisible, and change once it is ready. Then make the progress bar invisible.

  • I finally ended up using `View.INVISIBLE` instead of `View.GONE` like you said, but I don't see what difference does a RelativeLayout make here. – kaiser Feb 05 '17 at 14:50
  • Probably not a big difference. I just remembered that Google says in its documentation that the typical use case for FrameLayout is exactly one child. And RelativeLayout has often been attributed being efficient. –  Feb 05 '17 at 15:00
0
  1. You have a circular dependency problem - your FrameLayout height is wrap_content while your TextView height is match_parent. You may want to change your TextView height to wrap_content

  2. As @Disco mentioned in the comments, using View.INVISIBLE instead of View.GONE will probaly fix problem. When view is "gone", it just disappear from the view hierarchy, while when it is "invisible", it cannot being seen, but is still there logically, so layout dependent upon the invisible view are not effected by it's "disappearance".

Yoni Gross
  • 816
  • 8
  • 16
  • I initially thought that, but then I saw this -> http://stackoverflow.com/questions/4606613/combining-wrap-content-on-parent-and-fill-parent-on-child The answer there says it's a common case so that kind of circular dependency is ok in LinearLayout and FrameLayout. And yes, using INVISIBLE instead of GONE solves the issue. Thanks for the help – kaiser Feb 05 '17 at 14:45