Reputation: 83
I wanna set multiple layout dynamically one by one. how may i do this i used way but it shows only one layout with one textview.and i wanna set layout according to iteration of my loop. i used this code.
public class Show extends Activity
{
Context context=null;
TextView textView=null,textView1=null,textView2=null;
LinearLayout linearLayout1=null;
LinearLayout.LayoutParams linearlayoutParam=null;
protected void onCreate(Bundle state)
{
super.onCreate(state);
context=getApplicationContext();
int top=0;
for(int i=0;i<=3;i++)
{
textView=new TextView(context);
textView.setId(i);
textView.setText("Hii Priyank");
textView.setMinimumHeight(top);
textView.setGravity(Gravity.CENTER);
linearLayout1=new LinearLayout(context);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
linearLayout1.setId(i);
linearlayoutParam=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
linearlayoutParam.setMargins(10, top, 10, 10);
linearLayout1.setBackgroundColor(Color.BLACK);
top=top+10;
linearLayout1.setLayoutParams(linearlayoutParam);
LayoutInflater layoutinflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
linearLayout1.addView(textView, linearlayoutParam);
System.out.println(i);
}
setContentView(linearLayout1,linearlayoutParam);
}
It show this result only but i wanna set multiple.
it show this result only.
Upvotes: 0
Views: 206
Reputation: 2246
write linearLayout1=new LinearLayout(context);
before for
loop, and remove this from loop
edit
public class Show extends Activity
{
Context context=null;
TextView textView=null;
LinearLayout linearLayout1=null;
LinearLayout.LayoutParams linearlayoutParam=null;
protected void onCreate(Bundle state)
{
super.onCreate(state);
context=this;
LinearLayout.LayoutParams rootlayoutParam=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout rootLayout=new LinearLayout(context);
rootLayout.setLayoutParams(rootlayoutParam);
rootLayout.setOrientation(LinearLayout.VERTICAL);
int top=0;
for(int i=0;i<=3;i++)
{
textView=new TextView(context);
textView.setId(i);
textView.setText("Hii Priyank");
textView.setMinimumHeight(top);
textView.setGravity(Gravity.CENTER);
linearLayout1=new LinearLayout(context);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
linearLayout1.setId(i);
linearlayoutParam=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
linearlayoutParam.setMargins(10, top, 10, 10);
linearLayout1.setBackgroundColor(Color.BLACK);
top=top+10;
linearLayout1.setLayoutParams(linearlayoutParam);
LayoutInflater layoutinflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
linearLayout1.addView(textView, linearlayoutParam);
rootLayout.addView(linearLayout1);
System.out.println(i);
}
setContentView(rootLayout);
}
}
Upvotes: 1
Reputation: 914
Try this,
View linearLayout = new LinearLayout(context);
for(int i=0;i<=3;i++)
{
TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
params.setMargins(10,10,10,10);
valueTV.setLayoutParams(params);
((LinearLayout) linearLayout).addView(valueTV);
}
Upvotes: 0
Reputation: 2275
Context context = null;
TextView textView = null, textView1 = null, textView2 = null;
LinearLayout linearLayout1 = null;
LinearLayout.LayoutParams linearlayoutParam = null;
protected void onCreate(Bundle state) {
super.onCreate(state);
context = getApplicationContext();
int top = 0;
LinearLayout rootView = new LinearLayout(context);
rootView.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams rooParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i <= 3; i++) {
textView = new TextView(context);
textView.setId(i);
textView.setText("Hii Priyank");
textView.setMinimumHeight(top);
textView.setGravity(Gravity.CENTER);
linearLayout1 = new LinearLayout(context);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
linearLayout1.setId(i);
linearlayoutParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearlayoutParam.setMargins(10, top, 10, 10);
linearLayout1.setBackgroundColor(Color.BLACK);
top = top + 10;
linearLayout1.setLayoutParams(linearlayoutParam);
LayoutInflater layoutinflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
linearLayout1.addView(textView, linearlayoutParam);
rootView.addView(linearLayout1);
System.out.println(i);
}
setContentView(rootView, linearlayoutParam);
}
Upvotes: 0
Reputation: 18112
Create a LinearLayout
in your activity xml file. then do
setContentView(R.layout.linear_layout_xml);
Find the LinearLayout in the activity class as
LinearLayout ll = findViewById(R.id.linear_layout_id);
Then at the end of each loop use this
ll.addView(viewToBeAdded);
Upvotes: 1