-1

I have a frame layout that consists of a edittext and button (to remove the field) that i want to add multiple times to a relative layout when a user clicks a button.

I have searched but i just cant find how to do this programmatically.

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

    <EditText
        android:id="@+id/inputbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textSize="25sp"
        android:textStyle="bold"
        android:hint="@string/inputhint"
        android:ems="10"
        android:imeOptions="actionNext"
        android:singleLine="true" />

    <Button
        android:id="@+id/buttonremove"      
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_gravity="right|center_vertical"
        android:background="@drawable/remove" />

</FrameLayout>
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
QazPhilby
  • 117
  • 1
  • 2
  • 10
  • How do you want the resulting layout look like? All FrameLayouts in vertical row? – brillenheini Nov 23 '12 at 18:29
  • yes. so there will be a vertical row of text inputs. There are two non-dynamic inputs before and after the dynamically created ones using this framelayout – QazPhilby Nov 23 '12 at 20:32

1 Answers1

0

It is much easier to do, when the container is a LinearLayout with vertical orientation, not a RelativeLayout.

ViewGroup container = (ViewGroup) findViewById(R.id.container);
getLayoutInflater().inflate(R.layout.input, container, true);

Just retrieve the LinearLayout (container) and then inflate the layout with the container as parent. With attachToRoot set to true, it will automatically be added to the container.

brillenheini
  • 5,413
  • 3
  • 23
  • 20