Skip to content Skip to sidebar Skip to footer

Dynamically Adding Two Views One Below Other

I want to add two views one after the other, I used this way but I am getting an error. This is my XML.

Solution 1:

you are setting layout params to views dynamically in activity and these are new layout params objects, so your rule to add rel2 to below of rel1 gets cleared, instead try:

setContentView(R.layout.main);
        RelativeLayout rlstat1=(RelativeLayout)findViewById(R.id.rel1);
        RelativeLayout rlstat2=(RelativeLayout)findViewById(R.id.rel2);
        RelativeLayout.LayoutParams para1 = rlStat1.getLayoutParams();

        para1.width=LayoutParams.FILL_PARENT;
        para1.height=LayoutParams.FILL_PARENT;
         RelativeLayout.LayoutParams para2 = rlStat2.getLayoutParams();

        para2.width=LayoutParams.FILL_PARENT;
        para2.height=LayoutParams.FILL_PARENT;
        rlstat1.setLayoutParams(para1);
        rlstat1.addView(mView);

        para2.addRule(RelativeLayout.BELOW, R.id.rel1);
        rlstat2.addView(mView2);

Post a Comment for "Dynamically Adding Two Views One Below Other"