22

In a layout resource XML, I have 3 RelativeLayout(s) which are inside a main RelativeLayout. The view will be shown vertically. These 3 RelativeLayout() are set next to each other, and I want them to fill the whole screen, doesnt matter what will be the screen size. My, layout view:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backg"
 >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="@dimen/top_mr_image"
    android:src="@drawable/temp" />

<RelativeLayout
    android:id="@+id/r1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/imageView1"
    android:layout_marginLeft="10dp"
    android:background="@drawable/r1bg"
    android:layout_weight="1"

     >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/txt_mr_right"
        android:layout_marginRight="@dimen/txt_mr_right"
        android:layout_marginTop="39dp"
        android:text="S"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/textView1"
        android:layout_marginLeft="@dimen/txt_mr_right"
        android:layout_marginRight="@dimen/txt_mr_right"
        android:text="T"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

    <RelativeLayout
        android:id="@+id/r2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignTop="@+id/r1"
        android:layout_toRightOf="@+id/r1"
        android:layout_weight="1" >
    </RelativeLayout>

            <RelativeLayout
        android:id="@+id/r3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignTop="@+id/r2"
        android:layout_toRightOf="@+id/r2"
        android:layout_weight="1" >

    </RelativeLayout>

I set weight=1 and layout_width=0dp for each relativeLayout and this technique works with buttons, I thought the same will be with relativeLayout, seems my thoughts were wrong. Any idea?

UPD1: I have added an image of what I would like to have

enter image description here

Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55
Daler
  • 1,205
  • 3
  • 18
  • 39
  • I've done what i was looking for, thanks for comments – Daler Dec 23 '12 at 11:16
  • Please share that as an answer. Many curious fellows would like to know how did you achieve it. – Abhinav Saxena Jan 25 '19 at 05:38
  • Please check your question. It may mislead a reader that you want `FrameLayout` as a group view, where you want the children `RelativeLayout` of images to overlay one over the other. However, FYKI, if you can achieve this overlaying with `RelativeLayout` as well if all it's children have match_parent both on `layout_width` and `layout_height` without specifying the relation between each other. Orientation and Weighting are the properties of `LinearLayout`. – Abhinav Saxena Jan 25 '19 at 05:58

4 Answers4

25

RelativeLayout does not pay attention to android:layout_weight. (That's a property of LinearLayout.LayoutParams, but not of RelativeLayout.LayoutParams.)

You should be able to get the layout you want with a much simpler view hierarchy. It's not clear what you are trying to do, since the last two RelativeLayouts are empty. If you need a purely vertical organization, I'd suggest using LinearLayout instead of RelativeLayout.

EDIT Based on your edit, it looks like you want a horizontal layout of three compound views, each one clickable. I think something like the following will work:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <!-- First column -->
    <LinearLayout
        android:id="@+id/firstColumn"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="..." />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="text 1"
            . . . />
    </LinearLayout>

    <!-- Second column -->
    <LinearLayout . . . >
        . . .
    </LinearLayout>
</LinearLayout>

If the contents of the buttons aren't correct, you can replace the second-level LinearLayout views with RelativeLayout if that helps organize the layout better.

funkybro
  • 8,432
  • 6
  • 39
  • 52
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • I do have elements inside relativeLayout which has to be positioned in exact ways, it will be kind of a custom clickalble element with images and etc. As my coding knewledge is poor – Daler Dec 23 '12 at 07:03
  • @Daler - If you provide more details about the layout you want (a sketch or screen shot would be great), we can help you achieve it. Note also that a vertical `LinearLayout` can contain nested layouts to provide horizontal organization on whatever rows need it. – Ted Hopp Dec 23 '12 at 07:21
  • thanks for your feedback, i have updated my post you can see what i would like to have. I want to make each relativeLayout clickable, so they will act as a button. all three has the same design with different image types and texts – Daler Dec 23 '12 at 07:41
  • @Daler - I see you already accepted my answer, but I edited it to suggest how I would approach what you are trying to accomplish. – Ted Hopp Dec 23 '12 at 11:17
  • it is ok, i made it a little bit different, RelativeLayout->LinearLayout->Relativelayout. In my case i have to go this way as I have to position elements. – Daler Dec 23 '12 at 12:31
8

RelativeLayouts do not support weight. You need to use a LinearLayout as a parent container if you want to use weights.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • what if the parent is relativeLayout? and i can add the rest relativeLayouts inside the linearLayout. A little bit hardcore but possible – Daler Dec 23 '12 at 07:01
  • I guess you could have a RelativeLayout -> LinearLayout -> RelativeLayout x 3, but i'm not sure what that would accomplish. – dmon Dec 23 '12 at 07:02
0

Solution is very simple. I have been looking for weight distribution in relative layout.

It's a small trick for all these kind situations.

Use LinearLayout with android:orientation="horizontal"

Amit Patel
  • 1,795
  • 16
  • 20
  • 1
    Are you sure that `android:orientation` is a property of `RelativeLayout` ? Brother, that attribute belongs to `LinearLayout`. What about scrolling? That is given by `HorizontalScrollView`. Please at least elaborate the answer! – Abhinav Saxena Jan 25 '19 at 05:50
0

You can use Horizontally oriented LinearLayout Manager in the Recycler View, and place each RelativeLayout in each item, of its Adapter.

The Link: How to build a Horizontal ListView with RecyclerView?

If your RelativeLayouts are set to a fixed width and height, that is to the size of the Screen, that you can get from DisplayMatrics, that will be OK.

The Link: Get Screen width and height

If the contents of your RelativeLayouts are different, then you can use getItemViewType() method.

Please see: How to create RecyclerView with multiple view type?

Happy Coding :-)

Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55