4

I have a custom XML file. I want to repeat this in a layout (say Relative) n number of times, dynamically (obviously).

I have seen many posts, but none helped. I am not looking for a ListView or Adapters or so. It's as simple as - A RelativeLayout. Inside it, adding the custom XML one above another. Any number of times.

With a static LinearLayout (Vertical orientation), adding the view dynamically results in rendering it once, not one below another. Don't know why. Although a TextView or so do repeat one below the other in a loop inside a LinearLayout (Vertical).

Then I dynamically created the layout (Relative), and inflated the custom XML. Displayed one. When I tried for another below the first it told me to remove child's parent first (Exception). If I do that and add again, its as good as removing the first rendered view and adding it again.

So how can I get multiple views in same layout?

A rough presentation of what I've attempted:

 mainLayout = (RelativeLayout)findViewById(R.id.mainlay); //Mainlayout containing some views already

        params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW,R.id.sideLayout); //sideLayout is an existing LinearLayout within the main layout.



        View child = getLayoutInflater().inflate(R.layout.dynamiccustomlayout,null);

        RelativeLayout r1 = new RelativeLayout(this);

        r1.setLayoutParams(params);
        r1.addView(child);
        mainLayout.addView(r1);
        mainLayout.setLayoutParams(params);
        mainLayout.addView( child);
       /* r2 = new RelativeLayout(this);
        r2.setLayoutParams(params);
        r2.addView(contentLayout); [Gives exception] */ 
0

3 Answers 3

4

This is how it worked out for me...

Before that, the issue with android is:

If you add dynamic views inside a LinearLayout (Horizontal), they will appear horizontally with new created instances, added to the view.

However, shockingly, it's not the same in case of LinearLayout (Vertical orientation). Hence the whole mess.

Solution:

The RelativeLayout layout file was binded with the variable, somewhat like this:

customLay = (RelativeLayout) mainLay.findViewById(R.id.dynamicCustomLayout);

Then, a Dynamic RelativeLayout was created within which the former variable is added/wrapped.

customLayout = new RelativeLayout(this);
customLayout.addView(customLay);

Every layout is assigned an id:

 customLayout.setId(i);

And then a loop is run (2 if conditions for i=0 and i>0)

for i>0 (indicates the 2nd dynamic layout, to be added below the first), LayoutParameter is created:

params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

And then for i>0, using the ids of dynamic views, they are added one below the other:

//Following code below used id to place these views below each other to attain list type arrangement of views//

    // i==0 for first view on top//
     if (i == 0) { 
                        params.addRule(RelativeLayout.BELOW, R.id.sideLayout);
                        customLayout.setLayoutParams(params);
                    } 
   // i>0 for views that will follow the first top//
else { 
                        params.addRule(RelativeLayout.BELOW, i - 1);
                        customLayout.setLayoutParams(params);
                    }

Then added to main root layout, where all these views or cards need to be displayed:

includeLayout.addView(customLayout);

Ofcourse, the code is not just this. I have written the essential points that helped me achieve the target and that may help others in future.

So the main essence was ---

  1. using a Dynamic RelativeLayout, to
  2. bind the static RelativeLayout, and
  3. assigning ids to the Dynamic RelativeLayout wrappers, and
  4. on basis of ids use RelativeLayoutParameters to place the following ids below the previous ones.
Sign up to request clarification or add additional context in comments.

1 Comment

@2red13: Your answer may be right but I explained my situation how it failed for me. Your sample analog code was kinda bouncer to a beginner like me for that situation/time so I am kind of 'incapable' to judge on it, however I surely can say - Thank you very much for help. :)
1

You have to instanciate every child by itself

View child = getLayoutInflater().inflate(R.layout.dynamiccustomlayout,null);
r1.addView(child);
View child2 = getLayoutInflater().inflate(R.layout.dynamiccustomlayout,null);
r1.addView(child2);

//ok, i do a analog thing in obne of my apps. here is the code:

public class FlxForm extends LinearLayout {
    public FlxForm(Context context) {
      super(context);
      inflater = LayoutInflater.from(context);
      inflater.inflate(R.layout.flxform, this);
      this.setPadding(0, 0, 0, 0);
      container = (LinearLayout) this.findViewById(R.id.flxform);
      this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
      //here is my funtion to calculate the items i want to add, its a little bit too complicated, but in the end it works like:
      for(int i=0;i<10;i++){

         View x = inflater.inflate(R.layout.dynamiccustomlayout,null);
         container.addview(x);
     }
   }
}

XML for the Form

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flxform"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:background="@android:color/transparent"
    android:orientation="vertical" >
</LinearLayout>     

Then you can instantiate a "Form" Objekt and add it into a ScrollView

4 Comments

Read my comment in the code. If I add the child in the mainLayout, the child sits upon the previous views or overlaps them. That's the problem.
Another surprising aspect, the r1.setLayoutParam(params) didn't work this time. Just the custom view overriding or hiding the other existing views in the mainLayout.
But really keep in mind, the Problem with the remove childs parent first Exeption depends on the first part of my comment. In Your code you try to add the same child twiche and by the second try, it already has a parent because of the first adding. You will not have any problem with your code if yo add my first change suggestion
Sorry friend for the late response. I'd got a solution that time itself. Surely complex. Regarding your way, here's the thing I discovered. No matter how many child we create, I found, that its not the 'variable child' that has to be orphaned from the parent or re-related....but it's the xml file, the layout file. I found that time - even if I inflated other variable as a child, I still got the same error - why? Because the layout at core in the name of one child is already associated with the parent. Using other child variable is nothing but using other name to same layout. It faild
0

For doing this You would have to nest your RelativeLayout inside a ScrollView and Manage all the Scrolling, items adding, memory management, etc manually.

So the simple solution for adding n Number of Custom Views is to use a RecyclerView, ListView, GridView, etc with a neat CustomAdapter and Your Custom View.

Here is a nice example of using RecyclerView with custom Adapter :
http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465

I hope this Helps.

1 Comment

oh! I agree that it might be helpful but this is the first thing I wanted to avoid. THat's why I mentioned in the question that without Adapters (ScrollView too actually). But if nothing turns out I will turn out my way to this. One senior suggests to skip the static xml, rather create the xml dynamically too. And then use ids and use them in LayoutParameters to place below each other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.