how to adjust textview width based on images downloaded from internet

Viewed 51

I have a textview and a image view in linear layout with weight assigned. This linear layout is for list items. Problem is sometimes, the URL for image, is giving error, so Picasso library also returns to onError(). For such scenarios, I want that the text view shall occupy complete width instead of what is decided by its weight.

Layout snippet:

    <TextView
        android:id="@+id/content"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Small"
        android:layout_gravity="center_vertical"
        android:layout_weight="5" />
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/image"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:layout_weight="2"/>
</LinearLayout>

Picasso snippet in Adapter :

Picasso.with(mContext).load(mValues.get(position).getString("imageHref"))
    .into(holder.mImageView, new Callback() {
        @Override
        public void onSuccess() {
        }

        @Override
        public void onError() {
            holder.mContentView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;

            try {
                 holder.mContentView.setText(mValues.get(position).getString("description"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
});

This doesn't seems to work as the callback comes after some delay, and my textView is already updated with some value for textView width according to layout weight.

3 Answers
Related