Skip to content Skip to sidebar Skip to footer

How To Make A Relative Layout Duplicate Itself Inside A Linear Layout On Button Click

I have an alert dialogue set up inside a RelativeLayout. A small section is a ListView with a RelativeLayout inside. My xml file is as follows: Copy

Child Page

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#55934d"
android:padding="5dp"> 

<TextView 
    android:id="@+id/TextAddress"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TEXT ADDRESS:"
    android:textSize="10sp"
    android:textColor="#fff"
    />

</RelativeLayout>

And this is your activity.java code

public void addChildLayout(){
    //Inflater service
    LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //parent layout xml refrence
    LinearLayout linearLayout=(LinearLayout)findViewById(R.id.infolayout);
    //Child layout xml refrence
    View view=layoutInfralte.inflate(R.layout.infoitem, null);
    //add child to parent
    linearLayout.addView(view);
    }

Using the above pattern you can dynamically add new views.


Solution 2:

Another way of doing it would be to keep two copies of the relativelayout in the parent linearlayout . Keep the visibility of one of them as 'gone' and mark it visible when you want it to appear.

Benefit of this approach would be to minimize code.


Post a Comment for "How To Make A Relative Layout Duplicate Itself Inside A Linear Layout On Button Click"