0

I realize there is a question similar to this on stack overflow (getWidth() and getHeight() of View returns 0) that has gotten much attention. It seems like I may be calling getWidth too early, but how can I be sure that it does have an actual width though? I'm worried because I believe I may be getting a size of zero because of how I'm implementing an imageBitmap and not an imageResource.

My onCreate:

// Byte array to bitmap
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inMutable = true;
        mainImageBitmap = BitmapFactory.decodeByteArray(byteArray, 0,
                byteArray.length, options);

        // Set bitmap
        mainImageView.setImageBitmap(mainImageBitmap);
        Log.e("", mainImageView.getWidth() + " ");
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(400, 400); //Want to use getWidth and getHeight here, just hardcoding to 400 for now
        layout.setLayoutParams(params);
Community
  • 1
  • 1

1 Answers1

2

use GlobalLayout

example :

Global Layout Example

attach the Global layout to the view u want when the view will be ready it will callback to this function with width and height

in your case try using the next Code

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Community
  • 1
  • 1