1

I have a layout that contains 4 ImageViews with these id's: opp11, opp12, opp13, opp14. I inflate this layout using:

public class FourOptions extends LinearLayout {

public FourOptions(Context context, AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater layoutInflater = (LayoutInflater)context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.par_fouroptions_layout, this);

}

I do it like this:

fo = new FourOptions(this, null);
l.addView(fo);

l is a reference to the layout for which I add the above layout. this all works fine. I now try to add to l another view and to align this view with opp11. I do it like this:

    Root result = new Root(this, results[0], results[1], results[2], 45, -1, false); 
    //Root is a class that extends RelativeLayout
    RelativeLayout.LayoutParams paramsRoot = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    paramsRoot.addRule(RelativeLayout.ALIGN_LEFT, fo.findViewById(R.id.opp11).getId());
    paramsRoot.addRule(RelativeLayout.ALIGN_RIGHT, fo.findViewById(R.id.opp11).getId());
    paramsRoot.addRule(RelativeLayout.ALIGN_TOP, fo.findViewById(R.id.opp11).getId());
    paramsRoot.addRule(RelativeLayout.ALIGN_BOTTOM, fo.findViewById(R.id.opp11).getId());
    result.setLayoutParams(paramsRoot);
    l.addView(result);

result gets added to the top left corner of the screen as if no layout params were assigned to it. When I try to align result to views that are in l, the main layout, it works. That leads me to believe that the problem is with fo.findViewById(R.id.opp11).getId()

But I'm not sure. I have tried a few other ways. All failed. I'll appreciate suggestions.

This is par_fouroptions_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

     <ImageView
         android:id="@+id/opp11"
         android:contentDescription="@null"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/rectanglebuttons"
         android:gravity="center" />

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

<ImageView
     android:id ="@+id/opp12"
     android:contentDescription="@null"
     android:layout_width ="wrap_content"
     android:layout_height ="wrap_content" 
     android:gravity="center"
     android:src="@drawable/rectanglebuttons"
     />

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

 </LinearLayout>

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

<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

    <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

<ImageView
     android:id ="@+id/opp13"
     android:contentDescription="@null"
     android:layout_width ="wrap_content"
     android:layout_height ="wrap_content"
     android:gravity="center"
     android:src="@drawable/rectanglebuttons"
      />

 <View
    android:id="@+id/spaceview"
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

<ImageView
     android:id ="@+id/opp14"
     android:contentDescription="@null"
     android:layout_width ="wrap_content"
     android:layout_height ="wrap_content"
     android:gravity="center"
     android:src="@drawable/rectanglebuttons"
      />

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>
</LinearLayout>

Maor Yichyeh
  • 97
  • 1
  • 9

1 Answers1

1

Please review relative layout docs:

http://developer.android.com/guide/topics/ui/layout/relative.html

The behavior you are describing is exactly that which would be expected as I read these docs. I would key in on this line:

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. By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams.

Instead of adding result to root, add it to fo and you'll probably see the desired behavior.

Note this isn't a tree spanning description, rather it is "in relation to parent or each other" meaning that you can use params to position the new view either: In relation to the parent (root in your case) or its children (of).

Edit: You've added the xml for your fo layout and I think i see the issue:

You are wrapping op11 and op12 in a linear layout. In addition to the statements above regarding the parent child or child-child relationships of relative layouts, there is an added issue here.

Namely that what you want to do is ad a view to the wrapper layout which exists in your xml. You don't have an id on that view, however the block of code in question is:

<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

     <ImageView
         android:id="@+id/opp11"
         android:contentDescription="@null"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/rectanglebuttons"
         android:gravity="center" />

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

<ImageView
     android:id ="@+id/opp12"
     android:contentDescription="@null"
     android:layout_width ="wrap_content"
     android:layout_height ="wrap_content" 
     android:gravity="center"
     android:src="@drawable/rectanglebuttons"
     />

 <View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1" >
</View>

 </LinearLayout>

Since this linearLayout is a child of fo the above issue applies, however a second issue arises. Even if you were to add your view directly to this linear layout I think you would still see issues as you're trying to apply RelativeLayout rules to a linear layout. Change this view to a relative layout, modify accordingly, and then add your results view to this wrapper view and you should see what you want.

Nathaniel D. Waggoner
  • 2,856
  • 2
  • 19
  • 41
  • I have tried adding result to fo. Both by fo.addView(result) and by having fo's constructor receiving a Root object and placing it from with in class. Both ways yielded weird results (The images in par_fouroptions_layout.xml appeared smaller or only 2 of the 4 appeared...) – Maor Yichyeh Jan 06 '15 at 19:20
  • So i think that relates to your parameters, its going to try and perfectly align your view with the view you're referencing. They're probably overlapping. – Nathaniel D. Waggoner Jan 06 '15 at 19:28
  • Also those results aren't really weird at all - you're adding more content to the same sized view. Either the alloted space needs to increase, or everything is going to shrink to fit in the same space. – Nathaniel D. Waggoner Jan 06 '15 at 19:30
  • They are not nuts :) It's a way to centralize 2 objects - one to another, Both vertically and horizonatlly. – Maor Yichyeh Jan 06 '15 at 19:31
  • But I never got to see result in fo when I tried to add result to fo directly. The images size changed, but result wasn't there... – Maor Yichyeh Jan 06 '15 at 19:34
  • Fo is set to wrap_content, but you then try to make it the same size as op11 via aligning sides. Also par_fouroptions_layout is a linearLayout, not relative. The params you are applying to root are for locating it within par_fouroptions, but you're using relative layout parameters. I don't think that works. – Nathaniel D. Waggoner Jan 06 '15 at 19:39
  • The params centralize the 2 objects, not make them the same size. I use that often. It works nicely. Changing par_fouroptions_layout to relative layout doesn't solve the problem :( – Maor Yichyeh Jan 06 '15 at 19:53
  • You're trying to align all four sides. Either some of those are being ignored, or its going to mess with the sizes of your views to resolve the discrepancy. – Nathaniel D. Waggoner Jan 06 '15 at 19:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68297/discussion-between-maor-yichyeh-and-nathaniel-d-waggoner). – Maor Yichyeh Jan 06 '15 at 19:57