How to change the size of an Android imageView via code?

Asked

Viewed 915 times

2

I tried using Layoutparams but it didn’t work, I tried layoutParams.width = 80; and it didn’t work. Does anyone know how to do that? I thank you in advance.

In my case the picture got too big I needed to decrease.

My code:

ImageView iv = new ImageView(this);
iv.setImageDrawable(getResources().getDrawable(R.drawable.verde));
  • Since your image is in the folder drawable the indicated would be that it already had the desired dimensions. So it would not have to use processing and memory at runtime.

  • If however you really want to do this at runtime post the code when you tried to use layoutParams and explain what you mean by "it didn’t work".

1 answer

1

You can try that code:

ImageView imageView = findViewById(R.id.dl_image);
LayoutParams params = (LayoutParams) imageView.getLayoutParams();
params.width = 120;
imageView.setLayoutParams(params);

Or you can still use two Imageview properties in XML that are

//Posiciona a imagem ao centro do elemento ImageView
android:scaleType="fitCenter"

//Redimensiona a imagem para que caiba toda no elemento ImageView
android:adjustViewBounds="true"

I particularly prefer to use the second option because it is more practical.

Att. Jeiferson

Browser other questions tagged

You are not signed in. Login or sign up in order to post.