-1

i have some xml layout file.this is a my xml layout source code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RelativeLayout
    android:id="@+id/rot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#ff0000" >

    <ImageView
        android:id="@+id/btn_categorry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="12dp"
        android:background="@drawable/ic_launcher" />
</RelativeLayout>

i try to add new RelativeLayout programmatically,bellow my 'rot' layout.i wrote some code but i can't add new layout this is a my source

img.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            final RelativeLayout ll = new RelativeLayout(
                    getApplicationContext());

            Button add_btn = new Button(getApplicationContext());
            add_btn.setText("Click to add TextViiews and EditTexts");
            ll.addView(add_btn);

            parms.addRule(RelativeLayout.BELOW, R.id.rot);
            ll.setLayoutParams(parms);
        }
    });

as i said i can't add layout.what am i doing wrong? if anyone knows solution please help me thanks

chromelend
  • 295
  • 3
  • 9
  • 20

3 Answers3

1

You're not adding the layout to the root view. Call:

((ViewGroup)findViewById(R.id.rot)).addView(ll);
Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82
0

after this line:

 ll.setLayoutParams(parms);

added this code:

RelativeLayout root = (RelativeLayout).findViewById(R.id.rot);
root.addView(ll);
Naruto Uzumaki
  • 2,019
  • 18
  • 37
0

create object of your main Relative Layout.

       RelativeLayout main = (RelativeLayout)findViewById(R.id.mainRelativeLayout);
       main.addView(ll);

add above code after ll.setLayoutParams(parms);

Ravi
  • 34,851
  • 21
  • 122
  • 183