18

I'm trying to nest a RelativeLayout inside of a LinearLayout, and give the RelativeLayout a margin that spaces it's edges away from the edges of the LinearLayout. So far I have this:

LayoutInflater.from(context).inflate(R.layout.conversation_view_layout, this, true);

this.setLayoutParams(new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
int tbm = AllConversationsActivity.topAndBottomMargin;
int lrm = AllConversationsActivity.leftAndRightMargin;
((MarginLayoutParams) this.getLayoutParams()).setMargins(lrm, tbm, lrm, tbm);
this.requestLayout();

Which, if I'm reading the API correctly, should set the margin of the RelativeLayout to the numbers specified. It isn't doing so at all, but instead seems to be simply ignoring the setMargins() entirely.

Also, here's my xml

<merge xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView 
    android:id="@+id/conversation_picture"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"/>

<TextView 
    android:id="@+id/conversation_person"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@id/conversation_picture"
    android:singleLine="true"
    android:ellipsize="marquee"/>

<TextView 
    android:id="@+id/conversation_length"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"/>

<TextView 
    android:id="@+id/conversation_first_line"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@id/conversation_picture"
    android:layout_alignParentLeft="true"
    android:singleLine="true"
    android:ellipsize="end"/>

<TextView 
    android:id="@+id/conversation_last_time"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@id/conversation_first_line"
    android:layout_alignParentLeft="true"
    android:singleLine="true"
    android:ellipsize="marquee"/>

I have a small suspicion that these views are forcing the RelativeLayout edges to the parent edges when they make their alignment calls. Does someone know what has to be done to get these margins to set? Or maybe there's a better way to do this entirely? Appreciated.

blaineh
  • 2,263
  • 3
  • 28
  • 46

1 Answers1

36

Edit:

You are using MarginLayoutParams instead of LinearLayoutParams. NOTE: if you set LayoutParams on a layout container, you will need to use the parent's type.

So if you have a LinearLayout that contains a RelativeLayout, you need to set LinearLayout.LayoutParams on the RelativeLayout.

// NOT WORKING: LinearLayout.LayoutParams params = (LayoutParams) getLayoutParams();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
int tbm = AllConversationsActivity.topAndBottomMargin;
int lrm = AllConversationsActivity.leftAndRightMargin;
params.setMargins(tbm, lrm, tbm, lrm);
setLayoutParams(params);
Anshu Dwibhashi
  • 4,617
  • 3
  • 28
  • 59
Entreco
  • 12,738
  • 8
  • 75
  • 95
  • If this was the problem he would have been getting a class cast exception. MarginLayoutParams are a super class of LinearLayout.LayoutParams, RelativeLayout.LayoutParams and so on. Besides what setLayoutParams() actually does is to keep the params in a member field and call requestLayout() so only one of these calls should be hapenning(either requestLayout() or setLayoutParams() but not the two together) – asenovm Nov 17 '11 at 23:07
  • 2
    Entreco, you turned out to be exactly right, it was because I was creating the wrong owner of the params, but the implementation you put was slightly wrong. Here's the code that ended up working: `LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); int tbm = AllConversationsActivity.topAndBottomMargin; int lrm = AllConversationsActivity.leftAndRightMargin; params.setMargins(tbm, lrm, tbm, lrm); setLayoutParams(params); requestLayout();` Thanks for your help! – blaineh Nov 19 '11 at 20:57
  • this does not work for me. I tried this in my extended LinearLayout's constructor. The margins don't show up at all no matter what dimensions i provide. What could be wrong? – faizal Nov 09 '14 at 07:21
  • setLayoutParams(params) already calls requestLayout() by itself. – Aviv Ben Shabat Jan 27 '16 at 15:28