Creating Linear Layout with TextViews using a for loop

Posted by cad8 on Stack Overflow See other posts from Stack Overflow or by cad8
Published on 2011-01-02T02:43:46Z Indexed on 2011/01/02 2:54 UTC
Read the original article Hit count: 149

Filed under:
|
|
|

Hi all, 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 am asking:

LinearLayout MainLL= (LinearLayout) findViewById(R.id.myLayoutId); 

  for(int i=0; i<5; i++)
  {
   LinearLayout childLL= new LinearLayout(this);
   childLL.setOrientation(LinearLayout.VERTICAL);  
   childLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
   childLL.setGravity(Gravity.LEFT);

 TextView text = new TextView(this);
   text.setText("The Value of i is :"i);
   text.setTextSize(12);  
   text.setGravity(Gravity.LEFT);
   text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
childLL.addView(text);
MainLL.addView(childLL);
}

My problem is that I am only getting "The Value of i is :0" as the output, i.e. the first instance.

Any help would be much appreciated

© Stack Overflow or respective owner

Related posts about java

Related posts about android