0

When I try to change the width and height of my imageView the app crashes

Java code

final ImageView imageView1 = (ImageView)findViewById(R.id.imageView1);
imageView1.setLayoutParams(new LinearLayout.LayoutParams(50, 50));

The app crashes saying

08-22 15:14:33.670 E/AndroidRuntime(9917): 
java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams cannot be cast to
android.widget.RelativeLayout$LayoutParams
luiscosta
  • 855
  • 1
  • 10
  • 16

1 Answers1

1

Change

imageView1.setLayoutParams(new LinearLayout.LayoutParams(50, 50));

to

imageView1.setLayoutParams(new RelativeLayout.LayoutParams(50, 50));

The error occurs because your ImageView is inside a RelativeLayout, but you are trying to pass it LinearLayout params.

To keep your other layout modifications, you can try this:

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView1.getLayoutParams();
layoutParams.height = 50;
layoutParams.width = 50;
imageView1.setLayoutParams(layoutParams);
Squeazer
  • 1,234
  • 1
  • 14
  • 33