1

I has constraint layout with ImageView inside it:

<ImageView
        android:id="@+id/iv_lottery"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_lottery_info"
        tools:visibility="visible" />

In my fragment, when I recieve network response, I show ImageView and try load image from network via Picasso and resize it with autoheight.

mIvLottery.setVisibility(View.VISIBLE);
Picasso.get().load(mLottery.getBanner()).resize(mIvLottery.getWidth(), 0).into(mIvLottery);

The problem is: mIvLottery.getWidth() is 0.

How I can solve it?

If I change in xml layout android:visibility from gone to invisible or visible, all works fine. The problem is only when default visibility of ImageView is gone.

igor_rb
  • 1,821
  • 1
  • 19
  • 34

1 Answers1

2

Because mIvLottery visibility is set to GONE is perfectly fine that the width is 0.

You have several solutions here, but not knowing what your constraints are I'd say the quickest would be extracting the width from mLottery.getBanner().

Otherwise, if you need to calculate the width of a view not yet displayed on the screen you can rely on the measure() API.

So calling measure() and getMeasuredWidth() afterward should give you the width you are looking for. Bear in mind that measure accepts View.MeasureSpec as parameters, so it depends on the parent layout which ones to choose

mIvLottery.measure(UNSPECIFIED, AT_MOST);
int width = mIvLottery.getMeasuredWidth();
Sarpe
  • 5,747
  • 2
  • 28
  • 26
  • I call `mIvLottery.setVisibility(View.VISIBLE);` before call `getWidth();`. Width of item not calculated immediately after set item `visibility` to `View.VISIBLE`? Why? – igor_rb Feb 11 '19 at 08:35
  • Does the solution work? Changing the visibility of the view does not imply an immediate layout pass. But there are other APIs to force layout invalidation. – Sarpe Feb 11 '19 at 08:44
  • By the way, to force a view to be laid out, so it can be measured you can call `mIvLottery.forceLayout();` – Sarpe Feb 11 '19 at 08:53
  • 1
    `measure` takes [`MeasureSpec`](https://developer.android.com/reference/android/view/View.MeasureSpec) as parameters. `LayoutParams.WRAP_CONTENT` is invalid in this context. – Eugen Pechanec Feb 11 '19 at 09:17
  • Thanks for pointing that out @EugenPechanec, I always erroneously used layout params, and never had a problem. I'll be happy to edit my current answer, which are going to be the correct measures? UNSPECIFIED, AT_MOST? – Sarpe Feb 11 '19 at 09:56
  • The parent view is responsible for measuring and layout. We should ask "what would the parent layout put here". Right now only @igor_rp can guess. – Eugen Pechanec Feb 11 '19 at 10:01