1

how can I do to add child view programmatically to RelativeLayout at a given position?

For example, to reflect the following picture:

enter image description here

my xml: (com.example.TransparentSlidingDrawer child is a simple class that extends with LinearLayout)

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:orientation="horizontal" >
 <ScrollView 
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        >
<com.example.TransparentSlidingDrawer
    android:id="@+id/popup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
     >
     <RelativeLayout 
         android:id="@+id/relativelayout_imageview_textviews_slider"
         android:layout_width="wrap_content"
         android:layout_height="100dp"
         >    
     </RelativeLayout>


</com.example.TransparentSlidingDrawer>

</ScrollView> 
<ImageButton 
    android:id="@+id/open_close_slider"
    android:layout_width="25dp"
    android:layout_height="25dp"
    />


this is my function that this function does not work well :

private void addChild(){
    RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relativelayout_imageview_textviews_slider);

    RelativeLayout.LayoutParams imageparams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    imageparams.setMargins(0, 5, 0, 0);

    RelativeLayout.LayoutParams textparams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    textparams.setMargins(0, 25, 5, 0);

    imageparams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
    imageparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);

    for(int i = 0 ; i < 9 ; ++i){
        ImageButton btn = new ImageButton(getApplicationContext());
        btn.setId(i+1);
        btn.setBackgroundResource(R.drawable.ic_launcher);
        TextView txt = new TextView(getApplicationContext());
        txt.setText("text"+i);
        txt.setId(i+1);
        textparams.addRule(RelativeLayout.ALIGN_TOP, btn.getId());
        textparams.addRule(RelativeLayout.LEFT_OF, btn.getId());
        relativelayout.addView(btn, imageparams);
        relativelayout.addView(txt, textparams);
    }

}
2
  • what is a RelativeLayout inside your custom LinearLayout for? Commented Jan 26, 2014 at 21:45
  • for adding textviews and imageviews programmatically. Commented Jan 26, 2014 at 21:48

3 Answers 3

1

You are aligning the textviews with button tops and lefts here:

    textparams.addRule(RelativeLayout.ALIGN_TOP, btn.getId());
    textparams.addRule(RelativeLayout.LEFT_OF, btn.getId());

You need to do the same for the ImageButtons - except align the first one with the parent, then each subsequent one you need to align the top to the previous ImageButton (here is fake code):

    btn.addRule(RelativeLayout.ALIGN_BOTTOM, btnAbove.getId());

Then keep a reference to the previous button in "btnAbove" at the end of your loop like:

btnAbove = btn;

Sign up to request clarification or add additional context in comments.

4 Comments

you are telling the textviews to fill the parent.. they should probably WRAP_CONTENT not your code here -> RelativeLayout.LayoutParams textparams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT (problem), RelativeLayout.LayoutParams.FILL_PARENT (problem));
I changed it to warp_content but I have the same problem earlier.(i.sstatic.net/g8Ckw.jpg)
what is the problem? Also, you may need to create new params with each object - you keep adding rules to the same ones
0

Each view in the layout requires its own LayoutParams object. Reusing the same object won't work the way you want.

Also, prefer using an activity context for UI widgets to get your app theme properly applied.

Comments

0

You can also do this

final TextView titleView = (TextView) itemLayout
            .findViewById(R.id.titleView);

Rather than creating all those additional parameters this will take care of the position in the layout

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.