Setting width and height of an Imageview

Asked

Viewed 4,019 times

0

Hello, I need to change the size of a ImageView through the java class. And I don’t really know the command.

Follows my code

xml

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageViewVermelho"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

java class.

iv_vermelho = (ImageView) findViewById(R.id.imageViewVermelho);

I need to set the image size here

ex: if you were to do it in xml it would be like this

android:layout_width="104dp"
android:layout_height="104dp"

1 answer

1

To change parameters of a View (anyone who inherits it), you can use the native class LayoutParams.

In your case, to change the size of your ImageView dynamically, try something similar:

ImageView iv_vermelho = (ImageView) findViewById(R.id.imageViewVermelho);

//Acessando o LayoutParams de sua View
iv_vermelho.getLayoutParams().width = algumValor;
iv_vermelho.getLayoutParams().height = outroValor;
  • Friend that someoneValue; and otherValue; what is the measure I need to put in dp because when I put iv_red.getLayoutParams(). width = someValue; everything is uncoupled depending on the device resolution

  • 1

    @Klebersouza this value is given in pixel. You can get the equivalent value in dp with: pixelsValue / getResources().getDisplayMetrics().density;

Browser other questions tagged

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