how can I do to add child view programmatically to RelativeLayout at a given position?
For example, to reflect the following picture:
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);
}
}