Mothana
Mothana

Reputation: 41

image view getWidth() and getHeight() return 0 for kyocera phone

iam working on a project and i need the height an width for an image view , i am simply calling the getwidth() and getheight() normally like this

int targetW = mImageView.getWidth() ;
int targetH = mImageView.getHeight();

its working fine for all kind of android phones but when i am trying a kyocera hydro phone its return zero ! i am calling it after the image view rendered to the screen for sure because i am calling them when clicking on the view itself.

any suggestions ?

EDITED: Its working fine for all phones except for Kyocera Hydro phone

Upvotes: 0

Views: 364

Answers (1)

Nguyễn Hoài Nam
Nguyễn Hoài Nam

Reputation: 1140

try this

mImageView.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    Toast.makeText(getContext(), mImageView.getWidth() + "", Toast.DURATION_SHORT).show();

                    // remove OnGlobalLayoutListener, check Android version too
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
                        mImageView.getViewTreeObserver()
                                .removeOnGlobalLayoutListener(this);
                    else
                        mImageView.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(this);
                }
            });

Upvotes: 1

Related Questions