wrap_content is not working

Asked

Viewed 64 times

0

I’m having an error with wrap_content on android on my Imagebutton I set the image directly from a URL

EXAMPLE:

ImageButton img = (ImageButton) findViewById(R.id.imageButton);
Picasso.with(getContext()).load("https://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png").fit().into(img);

and the XML of this Imagebutton is as follows:

<ImageButton
                android:id="@+id/imageButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitEnd" />   

Image display I receive inserir a descrição da imagem aqui

  • Retire android:scaleType="fitEnd" or use android:scaleType="center"

  • @Ramaral didn’t work, I took the android:scaleType="fitEnd" and also tried to put the android:scaleType="center"

  • Remove the scaleType and add android:adjustViewBounds="true"

  • The adjustViewBounds (in API’s smaller than 17) does not "stretch" the image to occupy the entire Windows image if the image is smaller. For images larger than Imageview it "shrinks" correctly. You have to treat this if you are going to use this attribute.

  • @Márciooliveira I think that in this situation has no problem, since it is being used wrap_content

  • @ramaral, take a look: https://inthecheesefactory.com/blog/correct-imageview-adjustviewbounds-with-adjustable-imageview/en

  • @Márciooliveira Yeah, I know. The problem is that I don’t know if the AP wants the image to increase or get the size it has. But how does it say that fitCenter did not solve should be because he wants it to increase and in this case android:adjustViewBounds="true" only works for API17+.

  • @Márciooliveira am using API 26

Show 3 more comments

1 answer

1


The wrap_content in Imageviews only works if the image is already of known size, since the layout is inflated as soon as the Activity starts. And in your case your image will still be uploaded from the web.

My suggestion, since you are using Picasso, is to load the image as a Bitmap to a Target and use the callback method onBitmapLoaded() of the same to determine that the image is ready, then you still in this method can access the properties of width and height of the image and resize the Imageview according to the SetLayoutParams().

Something like that:

        Target target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                if (bitmap != null) {
                    int width = bitmap.getWidth();
                    int height = bitmap.getHeight();
                    // OBS: Crie um LayoutParams de acordo com o ViewGroup pai da ImageView. 
                    // No meu exemplo, estou supondo que a ImageView está dentro de um 
                    // FrameLayout.
                    imageView.setLayoutParams(new FrameLayout.LayoutParams(width, height));
                    imageview.setImageBitmap(bitmap);
                }
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {}

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {}
        };

        Picasso.with(this)
                .load(url)
                .into(target);
  • I tried with your code and in it the program crashes on the line of Picasso

  • Which line and which error?

  • I did yesterday, for a project of mine, something similar and worked, only using Glide instead of Picasso.

  • I’m taking the Url of the image in the comic, with the code that I posted in the question Imagebutton received the image but it did not increase, and with his it simply crashes in the part that calls the target inside Picasso

  • Strange. I took this code from Picasso’s own tutorials. I only edited the part of Onbitmaploaded(). It would be nice to post the exact mistake, because I don’t usually use Picasso. As I said, I use Glide more.

  • ah yes, sorry for so many changes I’m no longer with the error, I think I should use the resize() picasso

  • I’m new to Sopt, and also in android

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.