-1

I am working on an app which displays an image in the center. The xml code of the ImageView is below

<ImageView
    android:id="@+id/e_sym"
    android:layout_width="300px"
    android:layout_height="300px"
    android:rotation="0"
    android:contentDescription="@string/symbol_e"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.499"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.498"
    app:srcCompat="@drawable/symbol_e" />

I want to change the height and width from the java program on click of button.

decr.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //code to change height and width values of the ImageView
    }
});

The size has to be in pixel format

Sagar gujarati
  • 152
  • 1
  • 15
Uzair Mukadam
  • 21
  • 1
  • 4
  • 2
    Does this answer your question? [Set ImageView width and height programmatically?](https://stackoverflow.com/questions/3144940/set-imageview-width-and-height-programmatically) – Vipul Chauhan Dec 07 '19 at 08:32

2 Answers2

0

You have to set the height and width in LayoutParams. Check below:

decr.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //code to change height and width values of the ImageView

        imageView.getLayoutParams().height = 20;
        imageView.getLayoutParams().width = 20;
        imageView.requestLayout()
    }
});
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
  • @UzairMukadam, What's the problem? Why you reverse? Is there any problem in my answer? Point out my fault so that i can correct myself. Thanks – Md. Asaduzzaman Dec 10 '19 at 10:27
0

you just have to mention the width and the height of the imageView in LayoutParams.

decr.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //code to change height and width values of the ImageView

        imageView.getLayoutParams().height = 50; //can change the size according to you requirements
        imageView.getLayoutParams().width = 50; //--
        imageView.requestLayout()
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

    }
});
Namrata
  • 1,683
  • 1
  • 17
  • 28