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>