0

I want to dynamically add the relativelayout below the previous one everytime a button is clicked.I was able to achieve it for a single click.Please share the source code for that.

hotel_search.xml:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginBottom="60dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:background="#80212121">

    <TextView
        android:id="@+id/locationtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="30dp"
        android:text="Location"
        android:textColor="#BDBDBD"
        android:textSize="15sp" />

    <AutoCompleteTextView
        android:id="@+id/autohotellocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/locationtext"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:background="@android:color/transparent"
        android:text="Hotel Name/City/Code"
        android:textColor="#ffffff" />

    <View
        android:id="@+id/view1"
        android:layout_width="100dp"
        android:layout_height="0.5dp"
        android:layout_below="@+id/autohotellocation"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:background="#00838F"
        android:foregroundGravity="center"></View>

    <TextView
        android:id="@+id/checkin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/view1"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="10dp"
        android:text="CheckIn"
        android:textColor="#BDBDBD" />

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/locationtext"
        android:layout_alignStart="@+id/locationtext"
        android:layout_below="@+id/checkin"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp">

        <TextView
            android:id="@+id/checkindate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="18"
            android:textColor="#fff"
            android:textSize="23sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/checkindate"
            android:text="Jun 2015"
            android:textColor="#fff"
            android:textSize="15sp" />
    </RelativeLayout>

    <View
        android:id="@+id/view2"
        android:layout_width="100dp"
        android:layout_height="0.5dp"
        android:layout_below="@+id/relativeLayout"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:background="#00838F"
        android:foregroundGravity="center"></View>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view2"
        android:id="@+id/scrollView"
        android:scrollbars="vertical">

        <RelativeLayout
            android:id="@+id/rootlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/view2"
            android:layout_marginTop="30dp"
            android:scrollbars="vertical"
            >

            <RelativeLayout
                android:id="@+id/roomlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:scrollbars="vertical">


                <TextView
                    android:id="@+id/rooms"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/adults"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/rooms"

                    android:layout_toEndOf="@+id/button3"
                    android:layout_toRightOf="@+id/button3"
                    android:text="Adults"
                    android:textColor="#BDBDBD" />

                <Button
                    android:id="@+id/button3"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_below="@+id/adults"
                    android:layout_marginLeft="18dp"
                    android:layout_marginStart="18dp"
                    android:layout_marginTop="10dp"
                    android:layout_toEndOf="@+id/rooms"
                    android:layout_toRightOf="@+id/rooms"
                    android:background="#90006064"
                    android:text="-"
                    android:textColor="#fff"
                    android:textSize="22sp" />

                <Button
                    android:id="@+id/button4"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_alignLeft="@+id/adults"
                    android:layout_alignStart="@+id/adults"
                    android:layout_alignTop="@+id/button3"
                    android:layout_marginLeft="37dp"

                    android:layout_marginStart="37dp"
                    android:background="#90006064"
                    android:text="+"
                    android:textColor="#fff"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/children"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/button3"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_marginEnd="38dp"
                    android:layout_marginRight="38dp"
                    android:text="Children"
                    android:textColor="#BDBDBD" />

                <Button
                    android:id="@+id/button6"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginEnd="13dp"

                    android:layout_marginRight="13dp"
                    android:background="#90006064"
                    android:text="+"
                    android:textColor="#fff"
                    android:textSize="18sp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true" />

                <Button
                    android:id="@+id/button5"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:background="#90006064"
                    android:text="-"
                    android:textColor="#fff"
                    android:textSize="22sp"
                    android:layout_alignParentBottom="true"
                    android:layout_toLeftOf="@+id/button6"
                    android:layout_toStartOf="@+id/button6"
                    android:layout_marginRight="41dp"
                    android:layout_marginEnd="41dp" />
            </RelativeLayout>
        </RelativeLayout>
    </ScrollView>

    <TextView
        android:id="@+id/checkout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/checkin"
        android:layout_marginEnd="21dp"
        android:layout_marginRight="21dp"
        android:text="CheckOut"
        android:textColor="#BDBDBD" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkout"
        android:layout_alignStart="@+id/checkout"
        android:layout_alignTop="@+id/relativeLayout"
        android:id="@+id/relativeLayout2">

        <TextView
            android:id="@+id/checkoutdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="18"

            android:textColor="#fff"
            android:textSize="23sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/checkoutdate"
            android:text="Jun 2015"
            android:textColor="#fff"
            android:textSize="15sp" />
    </RelativeLayout>

    <View
        android:id="@+id/view3"
        android:layout_width="100dp"
        android:layout_height="0.5dp"
        android:layout_marginTop="20dp"
        android:background="#00838F"
        android:foregroundGravity="center"
        android:layout_below="@+id/scrollView"
        android:layout_toRightOf="@+id/checkin"
        android:layout_toEndOf="@+id/checkin"></View>

HotelSearch.java:

 public class HotelSearch extends Fragment {
    Button button;RelativeLayout relativeLayout,relativeLayout1;int counter=0;
 @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {

        View v=inflater.inflate(R.layout.hotel_search,container,false);

        relativeLayout=((RelativeLayout) v.findViewById(R.id.rootlayout));
        button=(Button) v.findViewById(R.id.addroom);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                relativeLayout1=new RelativeLayout(getContext());
                RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,20);
                layoutParams.addRule(RelativeLayout.BELOW,R.id.roomlayout);
                relativeLayout1.setBackgroundColor(Color.parseColor("#000000"));
                relativeLayout.addView(relativeLayout1, layoutParams);
                Log.e("Counter",String.valueOf(counter+1));


                    }


        });
        return v;
    }
}
jobin
  • 1,489
  • 5
  • 27
  • 52

2 Answers2

0

On your onClick method, where you create a new RelativeLayout() call setID() with some random int (You cant use a hardcoded one), then store it in a class field like private int lastId;

Once that is done, in the LayoutParams.addRule() call replace your R.id.roomlayout with your lastId field.

Hope you got it.

Nanoc
  • 2,381
  • 1
  • 20
  • 35
0

Simple solution would be make your parent layout as Linear Layout with orientation vertical now rest of the things in your code will be same with one addition in your code after thisrelativeLayout.addView(relativeLayout1, layoutParams); add this line 'linerLayout.addView(relativeLayout)';

Nikhil
  • 31
  • 3