[ACCEPTED]-RelativeLayout is taking fullscreen for wrap_content-android-layout

Accepted answer
Score: 271

From the RelativeLayout doc:

Class Overview

A Layout where 6 the positions of the children can be described 5 in relation to each other or to the parent.

Note 4 that you cannot have a circular dependency 3 between the size of the RelativeLayout and 2 the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM

Class documentation

Which is exactly 1 your case. RelativeLayout can not do that.

Score: 57

For those looking for a solution to this, like 3 I did, you can use FrameLayout instead of RelativeLayout.

Then you 2 can set the gravity the intended object 1 to bottom right as below

<TextView
    android:layout_gravity="bottom|right"
    android:text="FOOBARZ"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
</TextView>
Score: 23

You have set the RelativeLayout to "wrap_content" and 13 the TextView to android:layout_alignParentBottom="true", so it automatically tries 12 to stretch the RelativeLayout to the bottom. Don't 11 use such dependencies with Relative Layout, as 10 it can count as "circular dependencies".

From 9 the docs for RelativeLayout:

Note that you cannot have a circular dependency 8 between the size of the RelativeLayout and 7 the position of its children. For example, you 6 cannot have a RelativeLayout whose height 5 is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

Try to align 4 your TextView to something other than the 3 parent RelativeLayout, but watch out for 2 this problem as well:
Circular dependencies, need some help with exact code

Alternatively, try 1 to add more sophisticated inner layouts.

Score: 1

Dont use alight_Parent type properties with 3 the child views

You can use frame layout 2 instead of RelativeLayout with respective 1 gravity

    <FrameLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
     <TextView
        android:layout_gravity="bottom|right"
        android:text="Hello "
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

    </TextView>

</FrameLayout>
Score: 1

FrameLayout is usually good for placing different views 9 one on top of each other (where the most 8 recent child is on top of the previous child). In 7 your case, you'd like to place views one 6 next to each other (above, below, start, end), so 5 I think ConstrainLayout fits better because it's exactly 4 what it does.

Unlike RelativeLayout, you'd be able to set 3 the ConstrainLayout width to wrap_content and still arrange its children 2 views as you wish, for example instead of

android:layout_alignParentTop="true"

you 1 can use

grid:layout_constraintTop_toTopOf="parent" 

and instead of

android:layout_alignParentBottom="true"

you can use

grid:layout_constraintBottom_toBottomOf="parent"
Score: 0

Good answers. Now if you don't have layout_alignParentBottom="true" and 2 still getting this issue watch out for android:background="@drawable/bkgnd" where 1 bkgnd is a biggie.

Score: 0

I'm not sure why the clean and obvious way 15 of accomplishing this hasn't been posted 14 yet. This performant solution works for 13 any View MyView with a known height.

Wrap 12 your RelativeLayout with height wrap_content in a FrameLayout:

<!-- width here should constrain RelativeLayout -->
<FrameLayout 
     android:layout_width="@dimen/my_layout_width"
     android:layout_height="wrap_content">

     <RelativeLayout  
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />

     <MyView
        ...
        android:layout_gravity="bottom" />
</FrameLayout>

Just 11 note that the view at the bottom of the 10 FrameLayout will be on top of your RelativeLayout content, so 9 you'll need to add padding to the bottom 8 of that layout to accomodate it. If you 7 want that view to be variable height, you 6 can either Subclass FrameLayout to add padding 5 in code based on the measured view height, or 4 just change the FrameLayout to vertical 3 LinearLayout if you're not worried about 2 the performance, i.e. it's not a listview 1 item, or the views are relatively lightweight.

Score: 0

Not sure why all the answers here suggest 4 FrameLayout, which is designed to render a single view 3 or views layered in the z axis. OP's problem 2 is a sequence of views stacked vertically, which 1 should be in a LinearLayout.

More Related questions