Skip to content Skip to sidebar Skip to footer

Creating Linear Layout With Textviews Using A For Loop

I was wondering if there is a way to dynamically create an additional linear layout with a textview within a predefined liner layout. THis is my code so you get the gist of what I

Solution 1:

You don't need to wrap the TextView inside another LinearLayout, you can do just:

LinearLayout MainLL= (LinearLayout) findViewById(R.id.myLayoutId); 
  for(int i=0; i<5; i++){
 TextViewtext=newTextView(this);
   text.setText("The Value of i is :"+i); // <-- does it really compile without the + sign?
   text.setTextSize(12);  
   text.setGravity(Gravity.LEFT);
   text.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
MainLL.addView(text);
}

Solution 2:

Everything you are doing is correct just make it

childLL.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  

because your parent layout is filled by 1st view because of that you can not see other view.

and yes

 text.setText("The Value of i is :"+i); //add + sign

Solution 3:

Yes, if you greatly need to wrap another LinearLayout before wrapping the TextView. Please try this code:

childLL.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));  

This will ensure the wrapped LinearLayout has the same weight, so all the views will be displayed on the screen.

Post a Comment for "Creating Linear Layout With Textviews Using A For Loop"