Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

match_parent property for children in a RelativeLayout

In short, is it possible to tell a child in a RelativeLayout to always match the height of that RelativeLayout regardless of how other children in that same RelativeLayout behave? In short, that's it. Details below.

What I'm trying to achieve is a ListView row with at least three views, and one of those views would be kind of a stripe at the right side of the list entry (the red view below). The problem I'm having is that there is a TextView at the left side, and depeding on how much text I have, the stripe won't fill the whole layout. This is very clearly explained by images below.

ListView item layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"             android:layout_width="wrap_content"             android:layout_height="wrap_content">  <View         android:id="@+id/bottom_line"         android:layout_width="match_parent"         android:layout_height="5dp"         android:layout_alignParentBottom="true"         android:background="#fc0" />  <!-- Why match_parent does not work in view below? --> <View         android:id="@+id/stripe"         android:layout_width="80dp"         android:layout_height="wrap_content"         android:minHeight="50dp"         android:layout_alignParentRight="true"         android:layout_alignParentTop="true"         android:layout_alignParentBottom="true"         android:background="#f00" />  <TextView         android:id="@+id/text"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentLeft="true"         android:layout_toLeftOf="@id/stripe"         android:text="This is a very long line, meant to completely break the match_parent property of the box at right"         style="?android:textAppearanceLarge"/>  </RelativeLayout> 

Result:

RelativeLayout with child not filling the whole layout vertically

Setting stripe and root height to match_parent makes no difference. I did it.

Repeating the question, I want the red stripe to always fill the parent vertically. You can see stripe is not aligning to the top of root.

A note: the above example is the simplest, self-contained example I can think of. It's just a ListActivity with an anonymous ArrayAdapter populated by a static String array. The relevant code in onCreate is at most 8 lines, so no worries there. It's really the layout. Besides, I have this working with nested LinearLayouts already, but I'm trying to reduce the depth of my layout structure a bit, if possible. LinearLayout works fine, as expected.

like image 367
davidcesarino Avatar asked Aug 18 '13 20:08

davidcesarino


People also ask

What is Match_parent property in Layout_height?

match_parent and fill_parent are same property, used to define width or height of a view in full screen horizontally or vertically. These properties are used in android xml files like this. android:layout_width="match_parent" android:layout_height="fill_parent"

Which attribute or property is specific for RelativeLayout?

Positioning Views RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.

What is the difference between Match_parent and Fill_parent?

Conclusion : fill_parent and match_parent are the same, used when we want the height or width of a view to be as big as its parent view, fill_parent being deprecated. wrap_content is used when we want the view to occupy only as much space as required by it.

What is the difference between Match_parent and Wrap_content?

For each dimension, it can specify one of: FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding) WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding) an exact number.


2 Answers

I had the same requirement and my solution was to align the top of the red stripe to the parent's top, and the bottom of the stripe to the bottom of the text view on the left. The height in this case becomes irrelevant. You can either use wrap_content or match_parent.

like image 177
sergej shafarenka Avatar answered Oct 04 '22 06:10

sergej shafarenka


Simply put your main RelativeLayout inside a LinearLayout, then the calculation of child Views height will be correct . I had the same problem and it worked for me.

like image 32
Behzad Avatar answered Oct 04 '22 06:10

Behzad