View's getWidth() and getHeight() returning 0

You need to use the onSizechanged() method.

This is called during layout when the size of this view has changed


You should use: image1.getLayoutParams().width;


You are either accessing the ImageViews width and height before it has been measured, or after you set its visibility to GONE.

image2.setVisibility(View.GONE);

Then getWidth() or getHeight() will return 0.,

You can also have a look at onWindowFocusChanged(), onSizeChanged() and onMeasure():

 @Override
 public void onWindowFocusChanged(boolean focus) {
    super.onWindowFocusChanged(focus);
    // get the imageviews width and height here
 }