4

I have a xml file and i want to repeat a RelativeLayout portion programmatically in my code. How can i repeat the layout at run time?.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.11" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="39dp"
        android:layout_marginTop="26dp"
        android:text="Button" />

</RelativeLayout>

</LinearLayout>
user229044
  • 232,980
  • 40
  • 330
  • 338
sajjoo
  • 6,576
  • 20
  • 65
  • 86

3 Answers3

2
LayoutInflater vi = (LayoutInflater) currentContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.id, null);
parentview.add(v);
Triode
  • 11,309
  • 2
  • 38
  • 48
1

You can use LayoutInflater to instantiate XML layouts multiple times.

LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(R.layout.yourxml, null);
hovanessyan
  • 30,580
  • 6
  • 55
  • 83
  • any example or tutorial will be appreciative. i have been stuck here for 1 day. i am also new in android. – sajjoo Feb 22 '12 at 12:42
  • Thanks a lot actually i have been using the search engine but approach were different. – sajjoo Feb 22 '12 at 12:48
0

here you can find my answer about creating and populating a layout list on runtime.

Community
  • 1
  • 1
Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129