Search code examples
javaandroidxmlparametersandroid-relativelayout

How to add relative Layout below some element dynamically


Please help me to add a RelativeLayout below a RadioButton dynamically (I have to create RelativeLayout and RadioButton in class).

Something like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RadioButton
          android:id="@+id/radioButton3"
          android:text="RadioButton" />


    <RelativeLayout
          android:id="@+id/relativeLayout2"
          android:layout_below="@+id/radioButton3"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">

    </RelativeLayout>

</LinearLayout>

To View I can write:

myLayout.addView(myView, layoutParams);

But how about ViewGroup?


Solution

  • I am not sure whether I understand u. Please check it and give me feedback. Thank you!

        LinearLayout mLayout = (LinearLayout) findViewById(R.id.myLayout);
        LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        RadioButton mRadioButton = new RadioButton(this);
        mRadioButton.setLayoutParams(p);
        mLayout.addView(mRadioButton);
    
        p = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        RelativeLayout mRelativeLayout = new RelativeLayout(this);
        mRelativeLayout.setLayoutParams(p);
        mLayout.addView(mRelativeLayout);