3
for (int i = 0; i < rows; i++) {
            TableRow row =new TableRow(this);
            View v = new ImageView(getBaseContext());
            TextView tv_main =new TextView(this);
            ImageView iv = new imageView(this);                              
            tv_main.setText("Test");                
            iv.setImageDrawable(v.getResources().getDrawable(R.drawable.home_icon));
            row.addView(iv);
            row.addView(tv_main);
            table_layout.addView(row);
    }

How to set height and width programmatically? I had tried

iv.getLayoutParams().height=80;
iv.getLayoutParams().width=100;

but image is not display.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tufan
  • 2,789
  • 4
  • 34
  • 52
  • try http://stackoverflow.com/questions/4665853/how-to-populate-the-tablelayout-with-imageview-dynamically-in-android-jdk – George Thomas Feb 11 '15 at 12:21

1 Answers1

3

You need to create a new LayoutParams object and set the height and width for it and then pass it to the iv.setLayoutParams() method.

TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(100, 80); 
iv.setLayoutParams(layoutParams);
Androidz
  • 401
  • 1
  • 6
  • 19
  • This is also not working LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(120, 80)); iv.setLayoutParams(lp) ; – Tufan Feb 11 '15 at 12:12
  • I see you are adding the ImageView to a table row so please try to replace LinearLayout.LayoutParams with TableRow.LayoutParams. The child params should follow the parent type. – Androidz Feb 11 '15 at 12:15
  • i need 15 reputation to vote ..currently i have only 4 – Tufan Feb 11 '15 at 12:39
  • You can accept an answer for your questions on the left :) – Androidz Feb 11 '15 at 12:40