Question

I have a viewflipper with 12 linearlayouts in it(child views). These Linearlayouts are given ids as jan, feb, mar,... and so on. Now I wish to add a textview in each of these layouts programmatically. How do I do that? here's my code. Nor it gives any error neither does it show the textviews.

LinearLayout jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec;
jan = (LinearLayout)findViewById(R.id.jan);
feb = (LinearLayout)findViewById(R.id.feb);
mar = (LinearLayout)findViewById(R.id.mar);
//and so on...for all 12 months

//inside onCreate
for(int a=0;a<12;a++){
TextView txt = new TextView(TimelineActivity.this);
txt.setText("month : " + (a+1));
txt.setTextColor(0xadadad);
switch(a){
case 0:
jan.addView(txt);
break;
case 1:
feb.addView(txt);
break;
case 2:
mar.addView(txt);
break;
case 3:
apr.addView(txt);
break;
case 4:
may.addView(txt);
break;
case 5:
jun.addView(txt);
break;
case 6:
jul.addView(txt);
break;
case 7:
aug.addView(txt);
break;
case 8:
sep.addView(txt);
break;
case 9:
oct.addView(txt);
break;
case 10:
nov.addView(txt);
break;
case 11:
dec.addView(txt);
break;
}   

I am sure this is simple and I am making some silly mistake, but can't figure out what...

Was it helpful?

Solution

You'll need to call setLayoutParams(), as described in this answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top