Adding multiple views to RelativeLayout with loop - android

I'm trying to add multiple views to RelativeLayout that I declared inside xml. Looping is the only way because I do not know how many elements needed to add there, since the size is dynamic. But I got this error.
java.lang.IllegalStateException: The specified child already has a
parent. You must call removeView() on the child's parent first.
Code:
View inf = getActivity().getLayoutInflater().inflate(R.layout.item_table_edit, null);
RelativeLayout.LayoutParams layoutParamss = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < putzones.size(); i++) {
inf.setX((float) putzones.get(i).getPosX());
inf.setY((float) putzones.get(i).getPosY());
addMap.addView(inf, layoutParamss);
}
What is wrong here, since when I'm trying to add views by clicking items it works with the same code, without any error. code is same when I am adding by clicking item without loop. Thanks in advance.

You have to create a new instance of item_table_edit at each loop's iteration.
RelativeLayout.LayoutParams layoutParamss = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < putzones.size(); i++) {
View inf = getActivity().getLayoutInflater().inflate(R.layout.item_table_edit, null);
inf.setX((float) putzones.get(i).getPosX());
inf.setY((float) putzones.get(i).getPosY());
addMap.addView(inf, layoutParamss);
}

Related

Add Textview one under the other in loop

I'm trying to add textviews one just bellow another but when I run the code, it all gets stacked together. Here is the code:
RelativeLayout constraintLayout;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
private void createTable() {
RelativeLayout textRelativeLayout = new RelativeLayout(this);
relativeLayout.addView(textRelativeLayout);
for (int i = 1; i <= 5; i++) {
TextView textView = new TextView(this);
textView.setText("TextView " + String.valueOf(i));
setTextViewAttributes(textView);
textView.setId(i);
params.addRule(RelativeLayout.ALIGN_BOTTOM, textView.getId());
textRelativeLayout.addView(textView);
}
}
Since first I'm trying to make it work, I'm only setting dummy text for now. Is it my code that is wrong? Or did I miss a Param?
You didn't use param.
It should be textRelativeLayout.addView(textView , param);
Or use textView.setLayoutParams(params)
And I think the rule should be (I'm not tested this ):
params.addRule(RelativeLayout.BELOW, previousTextView.getId());
Also it is better to use a LinearLayout with vertical orientation to add view under other view.
You should use LinearLayout instead of RelativeLayout.
Make sure you set the orientation to 'vertical' for the LinearLayout as well.
No need to add rules to params like you're doing for the LinearLayout. They will get order from top to bottom automatically.

Xamarin dynamically added views using inflater inside loop adding only one entry

I followed the below link to dynamically add a layout multiple times using inflater and AddView()
Is there a way to programmatically create copies of a layout in android?
I used a loop to create multiple entries. But only one entry is comming up which is the result of last loop index
Below is my C# code
I can see only one child inside the parent which is the result of last loop.
What I missed?
var parent = FindViewById<RelativeLayout>(Resource.Id.ParentLayoutWrapper);
for (int i = 0; i < 4; i++)
{
var view = LayoutInflater.Inflate(Resource.Layout.RepeatingLayout, parent, false);
var txtView = view.FindViewById<TextView>(Resource.Id.textViewSample);
txtView.Text = i.ToString()+ " Android application is debugging";
txtView.Id = i;
parent.AddView(view, i);
}
The original post you worked from had a LinearLayout as the parent layout, not a RelativeLayout like you have. When you add a view (or another layout) to a LinearLayout, it gets positioned below (when LinearLayout has vertical orientation) any existing elements in the layout. However, the elements in a RelativeLayout need to use positioning properties to determine where they will be in the RelativeLayout, so every time you add the new layout, RepeatingLayout, since you are not changing the layout options, the view/layout is added over the existing view/layout. So change the parent layout to a LinearLayout in your layout file and then this should work:
LinearLayout parent = FindViewById<LinearLayout>(Resource.Id.parentLayout);
for (int i = 0; i < 4; i++)
{
var view = LayoutInflater.Inflate(Resource.Layout.RepeatingLayout, null);
var tv = view.FindViewById<TextView>(Resource.Id.textViewSample);
tv.Text = i.ToString() + " Android application is debugging";
parent.AddView(view);
}
Trying to do the same with a RelativeLayout as the parent layout highly complicates things unnecessarily.

Add child views in RelativeLayout programmatically

I have a function that loops over a list and returns the row elements.
I want to add these rows to an empty RelativeLayout one by one, so that all the rows are displayed sequentially vertically.
But the code below is not working.
void setRows(RelativeLayout rowContainer, ViewHolder viewHolder,
List<RowCollection> rowCollection) {
for (int i = 0; i < rowCollection.size(); i++) {
MyRow row = new MyRow(rowCollection.get(i));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, row.getId());
rowContainer.addView(row, params);
}
}
Only the last row element is displayed.
I guess but am not sure, that the rows are overlapping each other.
You are placing all your Views to the Bottom of the RelativeLayout, so they are always on top of the other. So there is always only the last one visible because the other ones are blow it.
Change your Code to this:
params.addRule(RelativeLayout.BELOW, iDFromPreviousView);
change to:
void setRows(RelativeLayout rowContainer, ViewHolder viewHolder,
List<RowCollection> rowCollection) {
MyRow lat_row=null;
for (int i = 0; i < rowCollection.size(); i++) {
MyRow row = new MyRow(rowCollection.get(i));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
if (i == 0) {
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
}else{
//<---get the id of previous row not sure what RowCollection contains
params.addRule(RelativeLayout.ABOVE,lat_row.getId());
}
lat_row=row;
rowContainer.addView(row, params);
}
}
this will stack all the view from bottom to top.

IllegalStateException thrown when calling ViewFlipper.addView for a second relative layout

I want to show more than two relative layouts in a viewFlipper, but running the following code throws an exception
java.lang.IllegalStateException: The specified child already has a
parent. You must call removeView() on the child's parent first.
Code:
for (int i = 0; i < imageid.length; i++)
{
RelativeLayout image = new RelativeLayout(MainActivity.this);
// image.addView(rrl3);
// image.addView(rrl4);
image.setBackgroundResource(imageid[i]);
vflp.addView(rrl3);
vflp.addView(image, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}

Android: Adding multiple Views to a Linear Layout dynamically

I checked the solution here:
Adding multiple views of the same type
Its given that, create a new View everytime you add it instead of changing only 1 view.
But i am doing this:
for (int i = 0; i < 10; i++) {
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(
CommentsActivity.LAYOUT_INFLATER_SERVICE);
View cv = vi.inflate(R.layout.item, null);
TextView textView1 = (TextView) cv.findViewById(R.id.tv1);
textView1.setText("-" + i);
TextView textView2 = (TextView) cv.findViewById(R.id.tv2);
textView2.setText("--" + i);
LinearLayout insertPoint = (LinearLayout) findViewById(R.id.layout);
insertPoint.addView(cv, 0, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
so its like creating a new inflater and view for every i. But i am only getting the last item.
ie.., only 1 inflatedView with tv1 as -9 and tv2 as --9
seems like everytime i go into the for loop, the old view is being replaced by the new view. How to add all the 10 views??
ThankYou
usually I use this
private void renewDetail(){
llDetail.removeAllViews();
for (int i = 0; i < 10; i++) {
llDetail.addView(new ChildDetailNotePieDiagram(context, "Name", 1000, 10));
}
}
the logic is first I clear all view from the parent layout and then add view to the parent layout.
where llDetail is a linear layout and I create a linear layout class ChildDetailNotePieDiagram and add it to the linear layout so basically it's a different solution from what you use now
but I think you can try my solution if you want :)
feel free to ask any question about this in the comment

Categories

Resources