Ziad H.
Ziad H.

Reputation: 689

getHeight() isn't returning correct value -using ViewTreeObserver- at first run

In MainActivity you click on a camera button, take a picture and then the image is shown on a PreviewActivity and then I should get the Width and Height of the imageView.

I tried to show a log message showing the height value when I touch the screen now the height value is corrected; so I think the problem is that the imageView isn't being laid out probably at first or something. "it got the height value before everything is laid right..", even though I'm using getViewTreeObserver().

    ViewTreeObserver viewTreeObserver = mImageView.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                setImage(mCurrentPhotoPath, selectedImageUri);
            }
        });
    }

Edit: I figured out that the problem is that the height value is taller than it acually is; because when opening the Camera app, the statusBar is hidden and it gets shown back again on PreviewActivity, but the height of the imageView is measured before statusBar is back up again, which causes the problem and causes the Height to be taller.

Upvotes: 1

Views: 206

Answers (2)

Ziad H.
Ziad H.

Reputation: 689

So, the Only way I was able to fix this problem is; if we got the image from the Camera (and if opened from MainActivity) then get the height and width after a little delay, so that everything is laid out probably.

//--             Get image path from previous activity
        mCurrentPhotoPath = getIntent().getStringExtra("imagePath");
        selectedImageUri = getIntent().getData();

        helper.setImage(mCurrentPhotoPath, selectedImageUri); //set image to imageView


        if (cameraOpenedFromMainActivity) {
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    //method to get height and width and do stuff
                }
            }, 500); //increase until everything is fine

        }else {
            ViewTreeObserver viewTreeObserver = mImageView.getViewTreeObserver();
            if (viewTreeObserver.isAlive()) {
                viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        mImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                        //method to get height and width and do stuff
                    }
                });
            }
        }
        //--

Upvotes: 1

Crowd
Crowd

Reputation: 21

you could try get the width in post method like:

mImageView.post(new Runnable(){

    @Override
    public void run() {
        mHeight = mImageView.getHeight(); //height is ready
    }
});

As I know, runnable task in post will be executed after view's measure and layout.

Upvotes: 2

Related Questions