1

This is what is causing me many problems:

<RelativeLayout
    android:id="@+id/galleryLayout"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:background="@color/white">

        <android.support.v4.view.ViewPager
            android:id="@+id/imageViewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true" />

        <com.viewpagerindicator.CirclePageIndicator
            android:id="@+id/circlePageIndicator"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:padding="@dimen/padding_middle"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"/>

</RelativeLayout>

Right now this code kind of works only because I've set RelativeLayout's height to 180dp but I don't want to have it this way. I want the whole thing to have height according to ViewPager's child.

There are exactly 2 problems if I set RelativeLayout's height to WrapContent.

Problem 1

ViewPager will expand throughout the whole screen. It just doesn't respect it's wrap_content height attribute. But I've partially solved that with this answer. I'd still appreciate if there's a better solution though.

Problem 2

I want have CirclePageIndicator on the bottom of it's parent (RelativeLayout) so I've added attribute layout_alignParentBottom="true" but now because of this, the RelativeLayout will expand throughout the whole screen for some reason.

So what I'm trying to have is a RelativeLayout which wraps around ViewPager which wraps around it's child. The child is downloaded from web so I can't pre-set it. And on the bottom of that RelativeLayout, I want to have a ViewPagerIndicator.

Community
  • 1
  • 1
Guy
  • 6,414
  • 19
  • 66
  • 136

1 Answers1

0

As for the problem 1 you solved the issue correctly, ViewPager will not wrap its children by default.

As for the second problem, this is a normal RelativeLayout behaviour. If you set its height to wrap_content and add two children, one with layout_alignParentTop="true" and second one with layout_alignParentBottom="true", they will stretch your layout height-wise.

What you should do is: ask yourself if you really need RelativeLayout. If you've provided the whole layout of yours I don't see a need for RelativeLayout (its costly). Vertical LinearLayout would do just fine. If you decide that you really need RelativeLayout, try changing your Indicator's rule from layout_alignParentBottom="true" to android:layout_below="@+id/imageViewPager".

Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • Thank you for your answer. The problem is that I want the Indicator "inside" the ViewPager, not below it. So I see no other way but to use RelativeLayout. – Guy Oct 05 '15 at 10:57
  • you can always use `FrameLayout` (`Indicator` with `layout_gravity="bottom"`) – Bartek Lipinski Oct 05 '15 at 10:58
  • Hm, that is an interesting idea. I haven't thought of that. I can't try this right now but I will as soon as I get home. I'll get back at you and accept your answer if it works. Thanks! – Guy Oct 05 '15 at 11:14