rafavinu
rafavinu

Reputation: 305

Dynamically create and add framelayouts android

I have created a framelayout having a button and 3 textviews inside the button. I want to duplicate this and add them dynamically through the java program one below the other.

The xml code for the same is attached.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <FrameLayout android:id="@+id/frameLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content">        
    <Button android:layout_width="319dp" android:layout_height="130dp"/>
    <TextView android:id="@+id/textView1" android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal" 
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content" android:text="@string/app_name"/>
    <TextView android:id="@+id/textView2" 
        android:layout_gravity="center_horizontal" 
        android:layout_marginTop="45dp"
        android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name"/>
    <TextView android:id="@+id/textView3" 
        android:layout_gravity="center_horizontal" 
        android:layout_marginTop="75dp"
        android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name"/>  
  </FrameLayout>
  <FrameLayout android:id="@+id/frameLayout2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_below="@id/frameLayout1">        
    <Button android:layout_width="319dp" android:layout_height="130dp"/>
    <TextView android:id="@+id/textView4" android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal" 
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content" android:text="@string/app_name"/>
    <TextView android:id="@+id/textView5" 
        android:layout_gravity="center_horizontal" 
        android:layout_marginTop="45dp"
        android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name"/>
    <TextView android:id="@+id/textView6" 
        android:layout_gravity="center_horizontal" 
        android:layout_marginTop="75dp"
        android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name"/>  
  </FrameLayout> 
</RelativeLayout>

The java code for this duplication and appending to the end is given by,

FrameLayout f2 = (FrameLayout)findViewById(R.id.frameLayout2);
        FrameLayout f3;
        f3 = f2;
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        p.addRule(RelativeLayout.BELOW, R.id.frameLayout2);
        f3.setLayoutParams(p);

I want the 3rd frame layout to be placed below the second one, but running this code, the second frame layout is also not visible, could anyone tell me where am i going wrong?

Upvotes: 0

Views: 2000

Answers (1)

aleb
aleb

Reputation: 2552

You have to extract the frame layout in a separate button_and_text.xml file and then use the include tag.

<RelativeLayout ...>
    <include layout="@layout/button_and_text" android:id="@+id/frameLayout1"/>
    <include layout="@layout/button_and_text" android:id="@+id/frameLayout2" android:layout_below="@id/frameLayout1"/>

Upvotes: 1

Related Questions