0

I need to set a part of my layout dynamically the number of times i receive a value of data. For example if receive data=3 I want that my same layout part appears three times but rest of the layout remains same. I tried inflating it but then it overlaps on my rest of the layout.

This is the layout I want to add recursively.

  link.xml

 <RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/offers"
  android:layout_below="@id/visits_remaining"
  android:layout_height="wrap_content"
  android:layout_width="match_parent" >


      <TextView 
          android:id="@+id/tier_name"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:text="Outlet Offer"/>

      <TextView 
          android:id="@+id/tier_visits"
          android:layout_alignParentRight="true"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:text="1 - 5 visits"/>

        <TextView 
            android:id="@+id/tv1"
            android:layout_below="@id/tier_name"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Hello"/>

        <ImageView
            android:id="@+id/v1"
            android:layout_below="@id/tier_name"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:src="@drawable/arrow" />

    <TextView 
         android:id="@+id/tv2"
         android:layout_below="@id/tv1"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="World"
         android:visibility="gone"/>
     <TextView 
          android:id="@+id/tv3"
          android:layout_below="@id/tv2"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:text="World"
          android:visibility="gone"/>
      <TextView 
           android:id="@+id/tv4"
           android:layout_below="@id/tv3"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:text="World"
           android:visibility="gone"/>
      <TextView 
            android:id="@+id/tv5"
            android:layout_below="@id/tv4"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="World"
            android:visibility="gone"/>

      </RelativeLayout>

This is the layout in which I want to add it above last two buttons.

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

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    <TextView
        android:id="@+id/outlet_names"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="14dp"
        android:textSize="25sp"
        android:text="Outlet Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <View 
         android:id="@+id/rule"
         android:layout_below="@id/outlet_names"
         android:layout_width="fill_parent"
         android:layout_marginTop="10dp"
         android:layout_height="0.02mm"
         android:background="#ffe6e1d4" />

     <TextView
         android:id="@+id/address"
         android:layout_marginTop="10dp"
         android:layout_below="@id/rule"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textSize="15sp"
         android:hint="address" />
     <TextView
         android:id="@+id/landline"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/address"
         android:textSize="15sp"
         android:autoLink="phone"
         android:hint="landline" />
     <TextView
         android:id="@+id/mobile1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/address"
         android:layout_toRightOf="@id/landline"
         android:layout_marginLeft="5dp"
         android:textSize="15sp"
         android:autoLink="phone"
         android:hint="mobile"/>
    <ImageView 
         android:id="@+id/outlets"
         android:layout_below="@id/mobile1"
         android:layout_height="100dp"
         android:layout_marginTop="7dp"
         android:layout_width="match_parent"/>

    <TextView
         android:id="@+id/prog_name"
         android:layout_below="@id/outlets"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginTop="10dp"
         android:textSize="15sp"
         android:text="Program Name" />

    <TextView 
         android:id="@+id/validity"
         android:layout_below="@id/prog_name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginTop="10dp"
         android:textSize="15sp"
         android:text="Program ends :"/>

     <TextView
         android:id="@+id/end"
         android:layout_below="@id/prog_name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
         android:layout_marginTop="10dp"
         android:text="Aug 5,2014"
         android:textSize="15sp" />


     <ProgressBar 
          android:id="@+id/progress"
          android:layout_below="@id/validity"
          style="?android:attr/progressBarStyleHorizontal"
          android:layout_marginTop="10dp"
          android:layout_width="fill_parent"
          android:layout_height="10dp"/>
      <TextView 
          android:id="@+id/visits_remaining"
          android:layout_below="@id/progress"
          android:layout_marginTop="10dp"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:text="2 visits to go for the next reward"/>         

      <Button
         android:id="@+id/more"
         android:layout_below="@+id/offers2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="See more" />

      <Button
         android:id="@+id/terms"
         android:layout_below="@id/more"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:text="Terms and Conditions" />

</RelativeLayout>

</ScrollView>

</RelativeLayout>
Gagan
  • 85
  • 1
  • 11

3 Answers3

1

place a ViewGroup in where you want in xml and add your view to it dynamically.
onclick a button or... below function call

final View addedView = getLayoutInflater().inflate(R.layout.someId,
                yourViewGroup, false);
        Button btn= (Button) addedView.findViewById(R.id.someId);
        ImageButton imgBtn= (ImageButton) addedView.findViewById(R.id.someId);
        addedView .setTag("a unique id");
        btn.setText("click me to do something");
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                int thisViewId = (Integer) addedView.getTag();//use this for indicate which view is click
                //do some thing
            }
        });

        imgBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                yourViewGroup.removeView(addedView);//delete just this addedView
            }
        });

        yourViewGroup.addView(addedView);  

main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" />
 <!--some tags here-->  
    <HorizontalScrollView
    android:id="@+id/doc_table"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#cccccc" >

       <LinearLayout
        android:id="@+id/table"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:animateLayoutChanges="true">
        <!--views will add here-->  
       </LinearLayout>
   </HorizontalScrollView>
 <!--some tags here-->  
</RelativeLayout>  

addedView.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical"
android:background="#eee" >

<Button
    android:id="@+id/chapter_page"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="3dp"
    android:text="" />

<ImageButton
    android:id="@+id/delete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/ic_delete"
    android:background="#00000000" />

</LinearLayout>  

if use above code you will see your Horizontal view get child on every time run function.

MHP
  • 2,613
  • 1
  • 16
  • 26
  • How should I place ViewGroup ? – Gagan Aug 12 '14 at 09:22
  • viewGroup can be or or ... in your xml that you should add android:id for it then in java do this: LinearLayout viewGroup = (LinearLayout) findViewById(R.id.yourId); . and then do like above code – MHP Aug 12 '14 at 10:02
  • I did the exact thing but it is getting superimposed. – Gagan Aug 12 '14 at 10:09
  • if you want to remove old addedView you can call yourViewGroup.removeAllViews() and then add new one each time that you want – MHP Aug 12 '14 at 10:19
  • I don't want to delete any I want to display all. – Gagan Aug 12 '14 at 10:50
0
Take linear layout on above button 

    <?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" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/outlet_names"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="14dp"
                android:text="Outlet Name"
                android:textSize="25sp" />

            <View
                android:id="@+id/rule"
                android:layout_width="fill_parent"
                android:layout_height="0.02mm"
                android:layout_marginTop="10dp"
                android:background="#ffe6e1d4" />

            <TextView
                android:id="@+id/address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:hint="address"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/mobile1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:hint="mobile"
                android:textSize="15sp" />

            <ImageView
                android:id="@+id/outlets"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginTop="7dp" />

            <TextView
                android:id="@+id/prog_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Program Name"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/validity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Program ends :"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/end"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Aug 5,2014"
                android:textSize="15sp" />

            <ProgressBar
                android:id="@+id/progress"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="fill_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp" />

            <TextView
                android:id="@+id/visits_remaining"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="2 visits to go for the next reward" />

            <LinearLayout
                android:id="@+id/linDynamic"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            </LinearLayout>

            <Button
                android:id="@+id/more"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="See more" />

            <Button
                android:id="@+id/terms"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Terms and Conditions" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>



    write in your main activty

        linDynamic = (LinearLayout) mView.findViewById(R.id.linDynamic);

            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            for (int i = 0; i < 4; i++)
            {

                    TextView txtDemo = new TextView(getActivity());
                    txtDemo .setTextSize(16);
                    txtDemo .setLayoutParams(lp);
                    txtDemo .setId(i);
                    lp.setMargins(0, 10, 0, 0);
                    txtDemo .setPadding(20, 10, 10, 10);
                    txtDemo .setText("Text View"+ i);
                    linDynamic .addView(txtDemo );

                }
            }
Jignesh Jain
  • 1,518
  • 12
  • 28
0

Add the ScrollView below where you want (in your case below the two buttons)

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"            
        >

        <LinearLayout
            android:id="@+id/add_dynamic_layout_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            />

    </ScrollView>

Then add the layout dynamically through code

//add dynamic layout below to the clicked layout
                final RelativeLayout newView = (RelativeLayout)getLayoutInflater().inflate(R.layout.dynamic_layout, null);
                newView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

                dynamicLayoutContainer.addView(newView);

                ImageView removeView = (ImageView)dynamicLayoutContainer.findViewById(R.id.dynamic_image_remove);
                removeView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dynamicLayoutContainer.removeView(newView);
                    }
                });

I hope this will help you.

Karthikeyan Ve
  • 2,550
  • 4
  • 22
  • 40