0

I have a parent layout in which I want to add a child layout onClick of another layout. The layout I want to add it's visibility is gone right now. After clicking on NextDestination layout it shows the another layout which is LinearAddedDestination. I want to add this layout when the user clicks on NextDestination layout till 14 times after 14 times the NextDestination layout should not be visible.

How can I achieve this??

GoBoxActivity Layout

<?xml version="1.0" encoding="utf-8"?>


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1.00"
            android:background="@android:color/white"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="DESTINATION"
                    android:id="@+id/textView14"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="20dp" />
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:orientation="horizontal"
                    android:background="@drawable/shape"
                    android:gravity="right"
                    android:layout_gravity="center|right"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="100dp"
                    android:id="@+id/LinearRemove"
                    android:visibility="gone">
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/ic_clear_black_18dp"
                        android:layout_marginLeft="05dp"
                        android:layout_marginTop="05dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="REMOVE"
                        android:id="@+id/textView47"
                        android:layout_gravity="center"
                        android:layout_marginLeft="05dp"
                        android:textSize="14sp"
                        android:layout_marginRight="15dp" />
                </LinearLayout>
            </LinearLayout>


            <ImageView
                android:layout_width="match_parent"
                android:layout_height="08dp"
                android:id="@+id/imageView14"
                android:background="@drawable/line2"/>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:id="@+id/imageView10"
                    android:layout_marginStart="20dp"
                    android:background="@drawable/ic_place_black_48dp"
                    android:layout_marginTop="05dp"
                    android:layout_gravity="center" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/Text_to"
                    android:drawableEnd="@drawable/ic_chevron_right_black_24dp"
                    android:layout_marginEnd="10dp"
                    android:text="@string/Loc"
                    android:layout_gravity="center"
                    android:layout_marginTop="05dp"
                    android:textAppearance="@android:style/TextAppearance.Medium"
                    android:cursorVisible="false"/>

            </LinearLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Street/Building/Place"
                android:layout_marginStart="48dp"/>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:id="@+id/imageView17"
                    android:layout_marginStart="20dp"
                    android:background="@drawable/ic_description_black_48dp"
                    android:layout_marginTop="10dp" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:id="@+id/editText_to_details"
                    android:layout_marginEnd="10dp"
                    android:hint="@string/LocationDetails"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/textViewHouseDetailsTo"
                android:layout_marginStart="48dp"
                android:hint="@string/HouseDetails" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:id="@+id/LinearLayoutAdditionalContactTo">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:id="@+id/imageView18"
                    android:layout_marginStart="20dp"
                    android:background="@drawable/ic_person_black_48dp"
                    android:layout_marginTop="05dp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/contactDetailsTo"
                    android:layout_marginEnd="10dp"
                    android:text="@string/additionalContact"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:drawableEnd="@drawable/ic_expand_more_black_24dp"
                    android:layout_marginTop="08dp"
                    android:layout_marginStart="05dp" />
            </LinearLayout>
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="60dp"
                android:layout_marginEnd="50dp"
                android:layout_gravity="center"
                android:visibility="gone"
                android:id="@+id/LinearLayoutTo"
                android:layout_marginTop="20dp">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/editTex"
                    android:layout_weight="1"
                    android:hint="Name"
                    android:layout_gravity="center"/>

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/editTe"
                    android:layout_weight="1"
                    android:hint="Phone"
                    android:layout_gravity="center"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/or"
                    android:id="@+id/textViewInputIfSender"
                    android:layout_gravity="center"
                    android:layout_marginTop="20dp" />

                <LinearLayout
                    android:layout_width="220dp"
                    android:layout_height="40dp"
                    android:background="@drawable/shape"
                    android:layout_gravity="center_horizontal">


                    <ImageButton
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:id="@+id/imageButton1"
                        android:background="@drawable/phone2"
                        android:layout_gravity="center"
                        android:layout_marginStart="30dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="@string/addFromContacts"
                        android:id="@+id/textView35"
                        android:layout_gravity="center"
                        android:layout_marginStart="20dp" />
                </LinearLayout>

            </LinearLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/textViewInputReceiver"
                android:layout_marginLeft="48dp"
                android:hint="@string/contact1"
                android:layout_marginTop="10dp" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:focusable="true"
                android:focusableInTouchMode="true">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:id="@+id/imageView12"
                    android:layout_marginStart="20dp"
                    android:background="@drawable/ic_description_black_48dp"
                    android:layout_marginTop="10dp" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:id="@+id/editText_from_details"
                    android:layout_marginEnd="10dp"
                    android:hint="Instruction"
                    android:layout_gravity="center"
                    android:layout_marginLeft="10dp" />

            </LinearLayout>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_weight="1.00"
                android:background="@android:color/white"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:id="@+id/LinearAddedDestination"
                android:visibility="gone">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:text="DESTINATION"
                        android:id="@+id/textView14"
                        android:layout_marginStart="20dp"
                        android:layout_marginTop="20dp" />
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:orientation="horizontal"
                        android:background="@drawable/shape"
                        android:gravity="right"
                        android:layout_gravity="center|right"
                        android:layout_marginTop="10dp"
                        android:layout_marginBottom="10dp"
                        android:layout_marginLeft="80dp"
                        android:id="@+id/LinearRemove">
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="@drawable/ic_clear_black_18dp"
                            android:layout_marginLeft="05dp"
                            android:layout_marginTop="05dp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:text="REMOVE"
                            android:id="@+id/textView47"
                            android:layout_gravity="center"
                            android:layout_marginLeft="05dp"
                            android:textSize="14sp"
                            android:layout_marginRight="15dp" />
                    </LinearLayout>
                </LinearLayout>
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="08dp"
                    android:id="@+id/imageView14"
                    android:background="@drawable/line2"/>
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView10"
                        android:layout_marginStart="20dp"
                        android:background="@drawable/ic_place_black_48dp"
                        android:layout_marginTop="05dp"
                        android:layout_gravity="center" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/Text_to"
                        android:drawableEnd="@drawable/ic_chevron_right_black_24dp"
                        android:layout_marginEnd="10dp"
                        android:text="@string/Loc"
                        android:layout_gravity="center"
                        android:layout_marginTop="05dp"
                        android:textAppearance="@android:style/TextAppearance.Medium"
                        android:cursorVisible="false"/>

                </LinearLayout>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Street/Building/Place"
                    android:layout_marginStart="48dp"/>
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView17"
                        android:layout_marginStart="20dp"
                        android:background="@drawable/ic_description_black_48dp"
                        android:layout_marginTop="10dp" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="45dp"
                        android:id="@+id/editText_to_details"
                        android:layout_marginEnd="10dp"
                        android:hint="@string/LocationDetails"/>

                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:id="@+id/textViewHouseDetailsTo"
                    android:layout_marginStart="48dp"
                    android:hint="@string/HouseDetails" />

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:id="@+id/LinearLayoutAdditionalContactTo">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView18"
                        android:layout_marginStart="20dp"
                        android:background="@drawable/ic_person_black_48dp"
                        android:layout_marginTop="05dp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/contactDetailsTo"
                        android:layout_marginEnd="10dp"
                        android:text="@string/additionalContact"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:drawableEnd="@drawable/ic_expand_more_black_24dp"
                        android:layout_marginTop="08dp"
                        android:layout_marginStart="05dp" />
                </LinearLayout>
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="60dp"
                    android:layout_marginEnd="50dp"
                    android:layout_gravity="center"
                    android:visibility="gone"
                    android:id="@+id/LinearLayoutTo"
                    android:layout_marginTop="20dp">

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/editTex"
                        android:layout_weight="1"
                        android:hint="Name"
                        android:layout_gravity="center"/>

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/editTe"
                        android:layout_weight="1"
                        android:hint="Phone"
                        android:layout_gravity="center"
                        />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="@string/or"
                        android:id="@+id/textViewInputIfSender"
                        android:layout_gravity="center"
                        android:layout_marginTop="20dp" />

                    <LinearLayout
                        android:layout_width="220dp"
                        android:layout_height="40dp"
                        android:background="@drawable/shape"
                        android:layout_gravity="center_horizontal">


                        <ImageButton
                            android:layout_width="20dp"
                            android:layout_height="20dp"
                            android:id="@+id/imageButton1"
                            android:background="@drawable/phone2"
                            android:layout_gravity="center"
                            android:layout_marginStart="30dp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:text="@string/addFromContacts"
                            android:id="@+id/textView35"
                            android:layout_gravity="center"
                            android:layout_marginStart="20dp" />
                    </LinearLayout>

                </LinearLayout>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:id="@+id/textViewInputReceiver"
                    android:layout_marginLeft="48dp"
                    android:hint="@string/contact1"
                    android:layout_marginTop="10dp" />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:focusable="true"
                    android:focusableInTouchMode="true">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView12"
                        android:layout_marginStart="20dp"
                        android:background="@drawable/ic_description_black_48dp"
                        android:layout_marginTop="10dp" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="45dp"
                        android:id="@+id/editText_from_details"
                        android:layout_marginEnd="10dp"
                        android:hint="Instruction"
                        android:layout_gravity="center"
                        android:layout_marginLeft="10dp" />

                </LinearLayout>

            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:orientation="horizontal"
                android:background="@drawable/shape"
                android:layout_gravity="right"
                android:layout_marginEnd="15dp"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp"
                android:id="@+id/LinearNextDestination">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ic_add_black_18dp"
                    android:layout_marginLeft="05dp"
                    android:layout_marginTop="05dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="NEXT DESTINATION"
                    android:id="@+id/textView47"
                    android:layout_gravity="center"
                    android:layout_marginLeft="05dp"
                    android:textSize="14sp"
                    android:layout_marginRight="15dp" />

            </LinearLayout>


        </LinearLayout>
  • Refer this link. Click [here]( http://stackoverflow.com/questions/17070047/how-to-set-layout-dynamically-in-android) It will give you a deep understanding of calling or bring the layout on top dynamically – Abish R Dec 15 '15 at 16:42
  • Thank you for reply. But mine is not another layout. It is a child layout inside parent layout which has an id it is not a xml layout to call with setContentView method.. @Abish R –  Dec 15 '15 at 16:54

1 Answers1

0

You are having many layout as the child layout for the main layout. But the better way to achieve your goal is to add fragment.

Using the fragment you can easily change the layouts view. Look at these links. Adding fragment will solve your problem.

http://developer.android.com/training/basics/fragments/creating.html

http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/

http://www.vogella.com/tutorials/AndroidFragments/article.html

http://www.learn2crack.com/2014/05/android-working-with-fragments.html

Using the fragment you can divide the space in the layout and add multiple fragments inside that.

Abish R
  • 1,537
  • 3
  • 18
  • 36
  • If you are having all the views inside a single layout xml file means you can use setVisibility of all the layout 'gone' and then needed layout 'visible'. You can change the visibility when an event or action happens. – Abish R Dec 15 '15 at 17:18