Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Imageview Height And Width Programmatically

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.

like image 269
Tufan Avatar asked May 27 '26 07:05

Tufan


1 Answers

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);
like image 139
Androidz Avatar answered Jun 03 '26 02:06

Androidz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!