Reputation: 11
I'm trying to dynamically build relative layouts consisting of several images. Theses relative layouts will be display below/rigth of previous relative layouts.
I'm starting with two relative layouts (rl100 and rl200). rl200 is below rl100. But rl200 isn't below rl100, is "stacked" over rl100. Can you help me ?
My code :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);
RelativeLayout rL100 = rlAdd(rLayout,100,-1,-1);
ImageButton imgBtn101 = imgBtnAdd(rL100,101,R.drawable.apricot,-1,-1);
ImageButton imgBtn102 = imgBtnAdd(rL100,102,R.drawable.banana,-1,101);
ImageButton imgBtn103 = imgBtnAdd(rL100,103,R.drawable.cherry,101,-1);
ImageButton imgBtn104 = imgBtnAdd(rL100,104,R.drawable.strawberry,102,103);
RelativeLayout rL200 = rlAdd(rLayout,200,100,-1);
ImageButton imgBtn201 = imgBtnAdd(rL100,201,R.drawable.pineapple,-1,-1);
ImageButton imgBtn202 = imgBtnAdd(rL100,202,R.drawable.pineapple,-1,201);
ImageButton imgBtn203 = imgBtnAdd(rL100,203,R.drawable.pineapple,201,-1);
ImageButton imgBtn204 = imgBtnAdd(rL100,204,R.drawable.pineapple,202,203);
}
private RelativeLayout rlAdd(RelativeLayout parentContainer, int nId, int nIdBelow,
int nIdRightOf) {
RelativeLayout rLayout = new RelativeLayout(this);
rLayout.setId(nId);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (nIdBelow != -1) {
rlp.addRule(RelativeLayout.BELOW, nIdBelow);
}
if (nIdRightOf != -1) {
rlp.addRule(RelativeLayout.RIGHT_OF, nIdRightOf);
}
rLayout.setLayoutParams(rlp);
parentContainer.addView(rLayout);
return rLayout;
}
private ImageButton imgBtnAdd(RelativeLayout ParentContainer, int ImgId, int ResDrawable,
int imgIdBelow, int imgIdRightOf) {
ImageButton imgBtn = new ImageButton(this);
imgBtn.setId(ImgId);
imgBtn.setImageResource(ResDrawable);
ParentContainer.addView(imgBtn);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imgBtn.getLayoutParams();
if (imgIdBelow != -1) {
lp.addRule(RelativeLayout.BELOW, imgIdBelow);
}
if (imgIdRightOf != -1) {
lp.addRule(RelativeLayout.RIGHT_OF, imgIdRightOf);
}
imgBtn.setLayoutParams(lp);
return imgBtn;
}
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<RelativeLayout android:id="@+id/rlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
</RelativeLayout>
</ScrollView>
Upvotes: 1
Views: 399
Reputation: 11
Thanks for your help. I make differents errors :
RelativeLayout rL200...
RelativeLayout rL200 = rlAdd(rLayout, 200, 100, -1);
Correct version : RelativeLayout rL200 = rlAdd(rLayout,200,100,-1);
ImageButton imgBtn20x
ImageButton imgBtn201 = imgBtnAdd(rL100,201,R.drawable.pineapple,-1,-1);
Correct version :
ImageButton imgBtn201 = imgBtnAdd(rL200,201,R.drawable.pineapple,-1,-1);
Same error for imgBtn202 ... imgBtn204
Errors 1 and 2 are stunning errors.
Third error in RelativeLayout android:id="@+id/rlayout" ...
Correct version :
<RelativeLayout android:id="@+id/rlayout" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="match_parent">
</RelativeLayout>
Upvotes: 0
Reputation: 770
You should use this line of code to add the relative layout with ID 200 below the relative layout with id 100, am I right?
RelativeLayout rL200 = rlAdd(rLayout, 200, 100, -1);
Upvotes: 1
Reputation: 6067
You are doing wrong over here
RelativeLayout rL200 = rlAdd(rLayout,100,-1,-1);
// From method
if (nIdBelow != -1) {
rlp.addRule(RelativeLayout.BELOW, nIdBelow);
}
As you are not passing id in nIdBelow field how it is supposed to add layout below. pass above image id or use Linear Layout with vertical orientation.
Upvotes: 1