1

Please consider ImageView inside RelativeLayout it has some specific layout params for RelativeLayout. I need to change the width and height of my ImageView programatically according to the screen size.

How can I save all layout parameters but only change width and height

Following code creates empty layout params but I need to save my old params and change only height and width.

   imageViewArticleImage.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mScreenSize.y / 3));

How can I achieve this ?

1 Answers1

0

Use getLayoutParams to get the current layout params. Change the width and height and go from there. IF the view is already visible, call requestLayout to force a new layout pass.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • How can I change width and height if existing layout params after `getLayoutParams` ? –  Feb 10 '16 at 22:16
  • 1
    By assigning to them. Width and height are public member variables of LayoutParams. You can assign them to either a pixel value or to WRAP_CONTENT or MATCH_PARENT. – Gabe Sechan Feb 10 '16 at 22:20
  • I cannot do this on existing layout params –  Feb 10 '16 at 23:04
  • 1
    Yes you can. I just did it this morning. ViewGroup.LayoutParams layoutParamsOverlay = mOverlayImage.getLayoutParams(); layoutParamsOverlay.width = (int) (mRemoteOverlayImage.getWidth() * scaleFactor); layoutParamsOverlay.height = (int) (mRemoteOverlayImage.getHeight() * scaleFactor); – Gabe Sechan Feb 10 '16 at 23:17
  • Thanks I will try. I have been searching for some method like setter) –  Feb 11 '16 at 00:04