I am adding a view to my table row programmatically. Everything is working fine and logcat isn't showing any errors. Strangely enough nothing is being displayed in my activity.
Below is the code I am using:
try{
int k=0;
for(i=0;i<=Math.ceil(jArray.length()/2);i++){
Log.e("I...",""+i+" "+Math.ceil(jArray.length()/2));
tr_head = new TableRow(getApplicationContext());
tr_head.setId(10+i);
tr_head.setPadding(30, 30, 30, 5);
tr_head.setGravity(Gravity.LEFT|Gravity.CENTER_HORIZONTAL);
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
for(j=0;j<2;j++)
{
if (k<jArray.length()){
jobj1 = jArray.getJSONObject(k);
LinearLayout parent = new LinearLayout(getApplicationContext());
parent.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(150), dpToPx(150)));
parent.setOrientation(LinearLayout.VERTICAL);
parent.setGravity(Gravity.CENTER);
ImageView img = new ImageView(getApplicationContext());
img.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(60),dpToPx(60)));
Picasso.with(getApplicationContext()).load(jobj1.getString("image")).resize(50,50).into(img);
parent.addView(img);
TextView txt = new TextView(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
txt.setLayoutParams(params);
txt.setText(jobj1.getString("name"));
parent.addView(txt);
tr_head.addView(parent);
parent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
String catid=jobj1.getString("category_id");
Intent intent = new Intent(getApplicationContext(), DescribeComplain.class);
intent.putExtra("category_id",catid);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
catch(JSONException je){
je.printStackTrace();
}
}
});
}
else{
break;
}
k++;
}
t1.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
}
catch(JSONException je){
je.printStackTrace();
}
}
});
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
After trying loads of methods, I solved it by setting linearLayout's parameters like this