Search code examples
androidandroid-layoutandroid-activityandroid-layoutparams

How to change width and height of existing layout params


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 ?


Solution

  • 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.