2

I have a LinearLayout with an ImageView inside. The ImageView is using layout_weight to take up half of the width of the LinearLayout:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1" />

</LinearLayout>

Now, what I want to do is make sure this ImageView is already a square. I can't simply set the height because the width is dynamic, depending on the width of the current screen. Is there a way I can make sure the height always matching the width of the view?

I've tried this:

imageView.getLayoutParams().height = imageView.getLayoutParams().width;
imageView.requestLayout();

But I get a NPE. Any ideas?

user1282637
  • 1,827
  • 5
  • 27
  • 56

0 Answers0